diff --git a/packages/runtime/src/helpers/functions.ts b/packages/runtime/src/helpers/functions.ts index 00324d549f..d397987eab 100644 --- a/packages/runtime/src/helpers/functions.ts +++ b/packages/runtime/src/helpers/functions.ts @@ -171,7 +171,8 @@ export const getApiRouteConfigs = async (publish: string, baseDir: string): Prom const pages = await readJSON(join(publish, 'server', 'pages-manifest.json')) const apiRoutes = Object.keys(pages).filter((page) => page.startsWith('/api/')) const pagesDir = join(baseDir, 'pages') - return Promise.all( + + return await Promise.all( apiRoutes.map(async (apiRoute) => { const filePath = getSourceFileForPage(apiRoute, pagesDir) return { route: apiRoute, config: await extractConfigFromFile(filePath), compiled: pages[apiRoute] } @@ -179,6 +180,16 @@ export const getApiRouteConfigs = async (publish: string, baseDir: string): Prom ) } +/** + * Looks for extended API routes (background and scheduled functions) and extract the config from the source file. + */ +export const getExtendedApiRouteConfigs = async (publish: string, baseDir: string): Promise> => { + const settledApiRoutes = await getApiRouteConfigs(publish, baseDir) + + // We only want to return the API routes that are background or scheduled functions + return settledApiRoutes.filter((apiRoute) => apiRoute.config.type !== undefined) +} + interface FunctionsManifest { functions: Array<{ name: string; schedule?: string }> } @@ -210,7 +221,7 @@ export const warnOnApiRoutes = async ({ if (functions.some((func) => func.schedule)) { console.warn( outdent` - ${chalk.yellowBright`Using scheduled API routes`} + ${chalk.yellowBright`Using scheduled API routes`} These are run on a schedule when deployed to production. 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. `, diff --git a/packages/runtime/src/index.ts b/packages/runtime/src/index.ts index 33723b3760..90ffcb43b6 100644 --- a/packages/runtime/src/index.ts +++ b/packages/runtime/src/index.ts @@ -23,7 +23,7 @@ import { generateFunctions, setupImageFunction, generatePagesResolver, - getApiRouteConfigs, + getExtendedApiRouteConfigs, warnOnApiRoutes, } from './helpers/functions' import { generateRedirects, generateStaticRedirects } from './helpers/redirects' @@ -152,7 +152,7 @@ const plugin: NetlifyPlugin = { const buildId = readFileSync(join(publish, 'BUILD_ID'), 'utf8').trim() await configureHandlerFunctions({ netlifyConfig, ignore, publish: relative(process.cwd(), publish) }) - const apiRoutes = await getApiRouteConfigs(publish, appDir) + const apiRoutes = await getExtendedApiRouteConfigs(publish, appDir) await generateFunctions(constants, appDir, apiRoutes) await generatePagesResolver({ target, constants }) diff --git a/test/__snapshots__/index.js.snap b/test/__snapshots__/index.js.snap index 88c6391bfc..e1a60553cd 100644 --- a/test/__snapshots__/index.js.snap +++ b/test/__snapshots__/index.js.snap @@ -1095,21 +1095,6 @@ Array [ "status": 200, "to": "/.netlify/functions/___netlify-handler", }, - Object { - "from": "/api/enterPreview", - "status": 200, - "to": "/.netlify/functions/_api_enterPreview-handler", - }, - Object { - "from": "/api/exitPreview", - "status": 200, - "to": "/.netlify/functions/_api_exitPreview-handler", - }, - Object { - "from": "/api/hello", - "status": 200, - "to": "/.netlify/functions/_api_hello-handler", - }, Object { "from": "/api/hello-background", "status": 200, @@ -1120,21 +1105,6 @@ Array [ "status": 404, "to": "/404.html", }, - Object { - "from": "/api/og", - "status": 200, - "to": "/.netlify/functions/_api_og-handler", - }, - Object { - "from": "/api/shows/:id", - "status": 200, - "to": "/.netlify/functions/_api_shows_id-PARAM-handler", - }, - Object { - "from": "/api/shows/:params/*", - "status": 200, - "to": "/.netlify/functions/_api_shows_params-SPLAT-handler", - }, Object { "force": false, "from": "/broken-image", diff --git a/test/index.js b/test/index.js index 6ad8755065..26eadaad87 100644 --- a/test/index.js +++ b/test/index.js @@ -13,7 +13,7 @@ const os = require('os') const cpy = require('cpy') const { dir: getTmpDir } = require('tmp-promise') const { downloadFile } = require('../packages/runtime/src/templates/handlerUtils') -const { getApiRouteConfigs } = require('../packages/runtime/src/helpers/functions') +const { getExtendedApiRouteConfigs } = require('../packages/runtime/src/helpers/functions') const nextRuntimeFactory = require('../packages/runtime/src') const nextRuntime = nextRuntimeFactory({}) @@ -1623,32 +1623,20 @@ describe('function helpers', () => { describe('api route file analysis', () => { it('extracts correct route configs from source files', async () => { await moveNextDist() - const configs = await getApiRouteConfigs('.next', process.cwd()) + const configs = await getExtendedApiRouteConfigs('.next', process.cwd()) // Using a Set means the order doesn't matter expect(new Set(configs)).toEqual( new Set([ - { compiled: 'pages/api/enterPreview.js', config: {}, route: '/api/enterPreview' }, { compiled: 'pages/api/hello-background.js', config: { type: 'experimental-background' }, route: '/api/hello-background', }, - { compiled: 'pages/api/exitPreview.js', config: {}, route: '/api/exitPreview' }, - { compiled: 'pages/api/shows/[...params].js', config: {}, route: '/api/shows/[...params]' }, - { compiled: 'pages/api/shows/[id].js', config: {}, route: '/api/shows/[id]' }, - { compiled: 'pages/api/hello.js', config: {}, route: '/api/hello' }, { compiled: 'pages/api/hello-scheduled.js', config: { schedule: '@hourly', type: 'experimental-scheduled' }, route: '/api/hello-scheduled', }, - { - compiled: 'pages/api/og.js', - config: { - runtime: 'experimental-edge', - }, - route: '/api/og', - }, ]), ) })