Skip to content

fix: only split extended routes to decrease build times #1731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/runtime/src/helpers/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,25 @@ 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] }
}),
)
}

/**
* 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<Array<ApiRouteConfig>> => {
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 }>
}
Expand Down Expand Up @@ -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.
`,
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
generateFunctions,
setupImageFunction,
generatePagesResolver,
getApiRouteConfigs,
getExtendedApiRouteConfigs,
warnOnApiRoutes,
} from './helpers/functions'
import { generateRedirects, generateStaticRedirects } from './helpers/redirects'
Expand Down Expand Up @@ -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 })
Expand Down
30 changes: 0 additions & 30 deletions test/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only generate custom rewrites for advanced API routes (background and scheduled functions).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are we mapping the other API routes to the SSR handler?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I understood of the previous implementation, the /api/* has a handler that catches all other requests.

     { 
       from: `${basePath}/api/*`, 
       to: HANDLER_FUNCTION_PATH, 
       status: 200, 
     },

See https://github.com/netlify/next-runtime/blob/673dfc09dc730f0ab91ee4ceec2ab9e8dd745266/packages/runtime/src/helpers/utils.ts#L147-L173

"from": "/api/hello-background",
"status": 200,
Expand All @@ -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",
Expand Down
16 changes: 2 additions & 14 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({})

Expand Down Expand Up @@ -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',
},
]),
)
})
Expand Down