Skip to content

Commit 898a8a7

Browse files
committed
feat: add warning logs for advanced api routes
1 parent 5354005 commit 898a8a7

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

packages/runtime/src/helpers/functions.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
/* eslint-disable max-lines */
12
import type { NetlifyConfig, NetlifyPluginConstants } from '@netlify/build'
23
import bridgeFile from '@vercel/node-bridge'
4+
import chalk from 'chalk'
35
import destr from 'destr'
4-
import { copyFile, ensureDir, readJSON, writeFile, writeJSON } from 'fs-extra'
6+
import { copyFile, ensureDir, existsSync, readJSON, writeFile, writeJSON } from 'fs-extra'
57
import type { ImageConfigComplete, RemotePattern } from 'next/dist/shared/lib/image-config'
8+
import { outdent } from 'outdent'
69
import { join, relative, resolve } from 'pathe'
710

811
import { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME, IMAGE_FUNCTION_NAME, DEFAULT_FUNCTIONS_SRC } from '../constants'
912
import { getApiHandler } from '../templates/getApiHandler'
1013
import { getHandler } from '../templates/getHandler'
1114
import { getPageResolver, getSinglePageResolver } from '../templates/getPageResolver'
1215

13-
import { ApiConfig, extractConfigFromFile } from './analysis'
16+
import { ApiConfig, ApiRouteType, extractConfigFromFile } from './analysis'
1417
import { getSourceFileForPage } from './files'
1518
import { getFunctionNameForPage } from './utils'
1619

@@ -36,7 +39,7 @@ export const generateFunctions = async (
3639
page: route,
3740
config,
3841
})
39-
const functionName = getFunctionNameForPage(route, config.type === 'experimental-background')
42+
const functionName = getFunctionNameForPage(route, config.type === ApiRouteType.BACKGROUND)
4043
await ensureDir(join(functionsDir, functionName))
4144
await writeFile(join(functionsDir, functionName, `${functionName}.js`), apiHandlerSource)
4245
await copyFile(bridgeFile, join(functionsDir, functionName, 'bridge.js'))
@@ -173,3 +176,43 @@ export const getApiRouteConfigs = async (publish: string, baseDir: string): Prom
173176
}),
174177
)
175178
}
179+
180+
interface FunctionsManifest {
181+
functions: Array<{ name: string; schedule?: string }>
182+
}
183+
184+
/**
185+
* Warn the user of the caveats if they're using background or scheduled API routes
186+
*/
187+
188+
export const warnOnApiRoutes = async ({
189+
FUNCTIONS_DIST,
190+
}: Pick<NetlifyPluginConstants, 'FUNCTIONS_DIST'>): Promise<void> => {
191+
const functionsManifestPath = join(FUNCTIONS_DIST, 'manifest.json')
192+
if (!existsSync(functionsManifestPath)) {
193+
return
194+
}
195+
196+
const { functions }: FunctionsManifest = await readJSON(functionsManifestPath)
197+
198+
if (functions.some((func) => func.name.endsWith('-background'))) {
199+
console.warn(
200+
outdent`
201+
${chalk.yellowBright`Using background API routes`}
202+
If your account type does not support background functions, the deploy will fail.
203+
During local development, background API routes will run as regular API routes, but in production they will immediately return an empty "202 Accepted" response.
204+
`,
205+
)
206+
}
207+
208+
if (functions.some((func) => func.schedule)) {
209+
console.warn(
210+
outdent`
211+
${chalk.yellowBright`Using scheduled API routes`}
212+
These are run on a schedule when deployed to production.
213+
You can test them locally by loading them in your browser but this will not be available when deployed, and any returned value is ignored.
214+
`,
215+
)
216+
}
217+
}
218+
/* eslint-enable max-lines */

packages/runtime/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ import {
1919
import { onPreDev } from './helpers/dev'
2020
import { writeEdgeFunctions, loadMiddlewareManifest, cleanupEdgeFunctions } from './helpers/edge'
2121
import { moveStaticPages, movePublicFiles, patchNextFiles } from './helpers/files'
22-
import { generateFunctions, setupImageFunction, generatePagesResolver, getApiRouteConfigs } from './helpers/functions'
22+
import {
23+
generateFunctions,
24+
setupImageFunction,
25+
generatePagesResolver,
26+
getApiRouteConfigs,
27+
warnOnApiRoutes,
28+
} from './helpers/functions'
2329
import { generateRedirects, generateStaticRedirects } from './helpers/redirects'
2430
import { shouldSkip, isNextAuthInstalled, getCustomImageResponseHeaders, getRemotePatterns } from './helpers/utils'
2531
import {
@@ -217,6 +223,7 @@ const plugin: NetlifyPlugin = {
217223

218224
warnForProblematicUserRewrites({ basePath, redirects })
219225
warnForRootRedirects({ appDir })
226+
await warnOnApiRoutes({ FUNCTIONS_DIST })
220227
},
221228
}
222229
// The types haven't been updated yet

0 commit comments

Comments
 (0)