From 948363c74e0557becaac5246b1cd122fc68b11ae Mon Sep 17 00:00:00 2001 From: Nick Taylor Date: Tue, 1 Nov 2022 14:04:57 -0400 Subject: [PATCH 1/4] fix: now only custom rewrites are created for background and scheduled functions --- packages/runtime/src/helpers/functions.ts | 7 ++++-- test/__snapshots__/index.js.snap | 30 ----------------------- test/index.js | 12 --------- 3 files changed, 5 insertions(+), 44 deletions(-) diff --git a/packages/runtime/src/helpers/functions.ts b/packages/runtime/src/helpers/functions.ts index 00324d549f..84addc8aff 100644 --- a/packages/runtime/src/helpers/functions.ts +++ b/packages/runtime/src/helpers/functions.ts @@ -171,12 +171,15 @@ 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( + + const settledApiRoutes = await Promise.all( apiRoutes.map(async (apiRoute) => { const filePath = getSourceFileForPage(apiRoute, pagesDir) return { route: apiRoute, config: await extractConfigFromFile(filePath), compiled: pages[apiRoute] } }), ) + + return settledApiRoutes.filter((apiRoute) => apiRoute.config.type !== undefined) } interface FunctionsManifest { @@ -210,7 +213,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/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..afc14972f6 100644 --- a/test/index.js +++ b/test/index.js @@ -1627,28 +1627,16 @@ describe('api route file analysis', () => { // 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', - }, ]), ) }) From 91ea2a67170bebee07db437d5496e58f2911e81e Mon Sep 17 00:00:00 2001 From: Nick Taylor Date: Tue, 1 Nov 2022 14:15:25 -0400 Subject: [PATCH 2/4] chore: renamed getApiRoutes to make it clearer what it is getting --- packages/runtime/src/helpers/functions.ts | 5 +++-- packages/runtime/src/index.ts | 4 ++-- test/index.js | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/runtime/src/helpers/functions.ts b/packages/runtime/src/helpers/functions.ts index 84addc8aff..f555d5d39c 100644 --- a/packages/runtime/src/helpers/functions.ts +++ b/packages/runtime/src/helpers/functions.ts @@ -165,9 +165,9 @@ export const setupImageFunction = async ({ } /** - * Look for API routes, and extract the config from the source file. + * Look for advanced API routes (background and scheduled functions), and extract the config from the source file. */ -export const getApiRouteConfigs = async (publish: string, baseDir: string): Promise> => { +export const getAdvancedApiRouteConfigs = async (publish: string, baseDir: string): Promise> => { 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') @@ -179,6 +179,7 @@ export const getApiRouteConfigs = async (publish: string, baseDir: string): Prom }), ) + // We only want to return the API routes that are background or scheduled functions return settledApiRoutes.filter((apiRoute) => apiRoute.config.type !== undefined) } diff --git a/packages/runtime/src/index.ts b/packages/runtime/src/index.ts index 33723b3760..cbda13d38d 100644 --- a/packages/runtime/src/index.ts +++ b/packages/runtime/src/index.ts @@ -23,7 +23,7 @@ import { generateFunctions, setupImageFunction, generatePagesResolver, - getApiRouteConfigs, + getAdvancedApiRouteConfigs, 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 getAdvancedApiRouteConfigs(publish, appDir) await generateFunctions(constants, appDir, apiRoutes) await generatePagesResolver({ target, constants }) diff --git a/test/index.js b/test/index.js index afc14972f6..cb4f3ebee1 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 { getAdvancedApiRouteConfigs } = require('../packages/runtime/src/helpers/functions') const nextRuntimeFactory = require('../packages/runtime/src') const nextRuntime = nextRuntimeFactory({}) @@ -1623,7 +1623,7 @@ 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 getAdvancedApiRouteConfigs('.next', process.cwd()) // Using a Set means the order doesn't matter expect(new Set(configs)).toEqual( new Set([ From ca48537afa00ed4f4934f84f1e07bc547b17d8f8 Mon Sep 17 00:00:00 2001 From: Nick Taylor Date: Tue, 1 Nov 2022 18:15:23 -0400 Subject: [PATCH 3/4] chore: renamed getAdvancedApiRputeConfigs to getExtendedApiRouteConfigs --- packages/runtime/src/helpers/functions.ts | 4 ++-- packages/runtime/src/index.ts | 4 ++-- test/index.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/runtime/src/helpers/functions.ts b/packages/runtime/src/helpers/functions.ts index f555d5d39c..9cc4de8d32 100644 --- a/packages/runtime/src/helpers/functions.ts +++ b/packages/runtime/src/helpers/functions.ts @@ -165,9 +165,9 @@ export const setupImageFunction = async ({ } /** - * Look for advanced API routes (background and scheduled functions), and extract the config from the source file. + * Look for extended API routes (background and scheduled functions) and extract the config from the source file. */ -export const getAdvancedApiRouteConfigs = async (publish: string, baseDir: string): Promise> => { +export const getExtendedApiRouteConfigs = async (publish: string, baseDir: string): Promise> => { 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') diff --git a/packages/runtime/src/index.ts b/packages/runtime/src/index.ts index cbda13d38d..90ffcb43b6 100644 --- a/packages/runtime/src/index.ts +++ b/packages/runtime/src/index.ts @@ -23,7 +23,7 @@ import { generateFunctions, setupImageFunction, generatePagesResolver, - getAdvancedApiRouteConfigs, + 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 getAdvancedApiRouteConfigs(publish, appDir) + const apiRoutes = await getExtendedApiRouteConfigs(publish, appDir) await generateFunctions(constants, appDir, apiRoutes) await generatePagesResolver({ target, constants }) diff --git a/test/index.js b/test/index.js index cb4f3ebee1..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 { getAdvancedApiRouteConfigs } = require('../packages/runtime/src/helpers/functions') +const { getExtendedApiRouteConfigs } = require('../packages/runtime/src/helpers/functions') const nextRuntimeFactory = require('../packages/runtime/src') const nextRuntime = nextRuntimeFactory({}) @@ -1623,7 +1623,7 @@ describe('function helpers', () => { describe('api route file analysis', () => { it('extracts correct route configs from source files', async () => { await moveNextDist() - const configs = await getAdvancedApiRouteConfigs('.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([ From 673dfc09dc730f0ab91ee4ceec2ab9e8dd745266 Mon Sep 17 00:00:00 2001 From: Nick Taylor Date: Wed, 2 Nov 2022 08:55:29 -0400 Subject: [PATCH 4/4] chore: created a separate function for getting extended API routes and their configs --- packages/runtime/src/helpers/functions.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/runtime/src/helpers/functions.ts b/packages/runtime/src/helpers/functions.ts index 9cc4de8d32..d397987eab 100644 --- a/packages/runtime/src/helpers/functions.ts +++ b/packages/runtime/src/helpers/functions.ts @@ -165,19 +165,26 @@ export const setupImageFunction = async ({ } /** - * Look for extended API routes (background and scheduled functions) and extract the config from the source file. + * Look for API routes, and extract the config from the source file. */ -export const getExtendedApiRouteConfigs = async (publish: string, baseDir: string): Promise> => { +export const getApiRouteConfigs = async (publish: string, baseDir: string): Promise> => { 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') - const settledApiRoutes = await 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] } }), ) +} + +/** + * 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)