Skip to content

Commit 1e6fb8c

Browse files
authored
fix: only split extended routes to decrease build times (#1731)
1 parent 45f31d5 commit 1e6fb8c

File tree

4 files changed

+17
-48
lines changed

4 files changed

+17
-48
lines changed

packages/runtime/src/helpers/functions.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,25 @@ export const getApiRouteConfigs = async (publish: string, baseDir: string): Prom
171171
const pages = await readJSON(join(publish, 'server', 'pages-manifest.json'))
172172
const apiRoutes = Object.keys(pages).filter((page) => page.startsWith('/api/'))
173173
const pagesDir = join(baseDir, 'pages')
174-
return Promise.all(
174+
175+
return await Promise.all(
175176
apiRoutes.map(async (apiRoute) => {
176177
const filePath = getSourceFileForPage(apiRoute, pagesDir)
177178
return { route: apiRoute, config: await extractConfigFromFile(filePath), compiled: pages[apiRoute] }
178179
}),
179180
)
180181
}
181182

183+
/**
184+
* Looks for extended API routes (background and scheduled functions) and extract the config from the source file.
185+
*/
186+
export const getExtendedApiRouteConfigs = async (publish: string, baseDir: string): Promise<Array<ApiRouteConfig>> => {
187+
const settledApiRoutes = await getApiRouteConfigs(publish, baseDir)
188+
189+
// We only want to return the API routes that are background or scheduled functions
190+
return settledApiRoutes.filter((apiRoute) => apiRoute.config.type !== undefined)
191+
}
192+
182193
interface FunctionsManifest {
183194
functions: Array<{ name: string; schedule?: string }>
184195
}
@@ -210,7 +221,7 @@ export const warnOnApiRoutes = async ({
210221
if (functions.some((func) => func.schedule)) {
211222
console.warn(
212223
outdent`
213-
${chalk.yellowBright`Using scheduled API routes`}
224+
${chalk.yellowBright`Using scheduled API routes`}
214225
These are run on a schedule when deployed to production.
215226
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.
216227
`,

packages/runtime/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
generateFunctions,
2424
setupImageFunction,
2525
generatePagesResolver,
26-
getApiRouteConfigs,
26+
getExtendedApiRouteConfigs,
2727
warnOnApiRoutes,
2828
} from './helpers/functions'
2929
import { generateRedirects, generateStaticRedirects } from './helpers/redirects'
@@ -152,7 +152,7 @@ const plugin: NetlifyPlugin = {
152152
const buildId = readFileSync(join(publish, 'BUILD_ID'), 'utf8').trim()
153153

154154
await configureHandlerFunctions({ netlifyConfig, ignore, publish: relative(process.cwd(), publish) })
155-
const apiRoutes = await getApiRouteConfigs(publish, appDir)
155+
const apiRoutes = await getExtendedApiRouteConfigs(publish, appDir)
156156

157157
await generateFunctions(constants, appDir, apiRoutes)
158158
await generatePagesResolver({ target, constants })

test/__snapshots__/index.js.snap

-30
Original file line numberDiff line numberDiff line change
@@ -1095,21 +1095,6 @@ Array [
10951095
"status": 200,
10961096
"to": "/.netlify/functions/___netlify-handler",
10971097
},
1098-
Object {
1099-
"from": "/api/enterPreview",
1100-
"status": 200,
1101-
"to": "/.netlify/functions/_api_enterPreview-handler",
1102-
},
1103-
Object {
1104-
"from": "/api/exitPreview",
1105-
"status": 200,
1106-
"to": "/.netlify/functions/_api_exitPreview-handler",
1107-
},
1108-
Object {
1109-
"from": "/api/hello",
1110-
"status": 200,
1111-
"to": "/.netlify/functions/_api_hello-handler",
1112-
},
11131098
Object {
11141099
"from": "/api/hello-background",
11151100
"status": 200,
@@ -1120,21 +1105,6 @@ Array [
11201105
"status": 404,
11211106
"to": "/404.html",
11221107
},
1123-
Object {
1124-
"from": "/api/og",
1125-
"status": 200,
1126-
"to": "/.netlify/functions/_api_og-handler",
1127-
},
1128-
Object {
1129-
"from": "/api/shows/:id",
1130-
"status": 200,
1131-
"to": "/.netlify/functions/_api_shows_id-PARAM-handler",
1132-
},
1133-
Object {
1134-
"from": "/api/shows/:params/*",
1135-
"status": 200,
1136-
"to": "/.netlify/functions/_api_shows_params-SPLAT-handler",
1137-
},
11381108
Object {
11391109
"force": false,
11401110
"from": "/broken-image",

test/index.js

+2-14
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const os = require('os')
1313
const cpy = require('cpy')
1414
const { dir: getTmpDir } = require('tmp-promise')
1515
const { downloadFile } = require('../packages/runtime/src/templates/handlerUtils')
16-
const { getApiRouteConfigs } = require('../packages/runtime/src/helpers/functions')
16+
const { getExtendedApiRouteConfigs } = require('../packages/runtime/src/helpers/functions')
1717
const nextRuntimeFactory = require('../packages/runtime/src')
1818
const nextRuntime = nextRuntimeFactory({})
1919

@@ -1623,32 +1623,20 @@ describe('function helpers', () => {
16231623
describe('api route file analysis', () => {
16241624
it('extracts correct route configs from source files', async () => {
16251625
await moveNextDist()
1626-
const configs = await getApiRouteConfigs('.next', process.cwd())
1626+
const configs = await getExtendedApiRouteConfigs('.next', process.cwd())
16271627
// Using a Set means the order doesn't matter
16281628
expect(new Set(configs)).toEqual(
16291629
new Set([
1630-
{ compiled: 'pages/api/enterPreview.js', config: {}, route: '/api/enterPreview' },
16311630
{
16321631
compiled: 'pages/api/hello-background.js',
16331632
config: { type: 'experimental-background' },
16341633
route: '/api/hello-background',
16351634
},
1636-
{ compiled: 'pages/api/exitPreview.js', config: {}, route: '/api/exitPreview' },
1637-
{ compiled: 'pages/api/shows/[...params].js', config: {}, route: '/api/shows/[...params]' },
1638-
{ compiled: 'pages/api/shows/[id].js', config: {}, route: '/api/shows/[id]' },
1639-
{ compiled: 'pages/api/hello.js', config: {}, route: '/api/hello' },
16401635
{
16411636
compiled: 'pages/api/hello-scheduled.js',
16421637
config: { schedule: '@hourly', type: 'experimental-scheduled' },
16431638
route: '/api/hello-scheduled',
16441639
},
1645-
{
1646-
compiled: 'pages/api/og.js',
1647-
config: {
1648-
runtime: 'experimental-edge',
1649-
},
1650-
route: '/api/og',
1651-
},
16521640
]),
16531641
)
16541642
})

0 commit comments

Comments
 (0)