Skip to content

Commit 3b2def8

Browse files
committed
fix: add activation guard for splitApiRoutes
1 parent 7f99f89 commit 3b2def8

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

packages/runtime/src/helpers/flags.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import destr from 'destr'
2+
import { existsSync } from 'fs-extra'
3+
import { join } from 'pathe'
24

35
/**
46
* If this flag is enabled, we generate individual Lambda functions for API Routes.
@@ -11,7 +13,19 @@ import destr from 'destr'
1113
* If disabled, we bundle all API Routes into a single function.
1214
* This is can lead to large bundle sizes.
1315
*
16+
* Relies on `next-server.js.nft.json`, which is only supported in Next.js 12+.
17+
*
1418
* Disabled by default. Can be overriden using the NEXT_SPLIT_API_ROUTES env var.
1519
*/
16-
export const splitApiRoutes = (featureFlags: Record<string, unknown>): boolean =>
17-
destr(process.env.NEXT_SPLIT_API_ROUTES) ?? featureFlags.next_split_api_routes ?? false
20+
export const splitApiRoutes = (featureFlags: Record<string, unknown>, publish: string): boolean => {
21+
const isEnabled = destr(process.env.NEXT_SPLIT_API_ROUTES) ?? featureFlags.next_split_api_routes ?? false
22+
23+
if (isEnabled && !existsSync(join(publish, 'next-server.js.nft.json'))) {
24+
console.warn(
25+
'Trace-based bundling not possible on this version of Next.js. Speed up your builds significantly by upgrading to Next.js v12.',
26+
)
27+
return false
28+
}
29+
30+
return isEnabled
31+
}

packages/runtime/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ const plugin: NetlifyPlugin = {
166166

167167
const buildId = readFileSync(join(publish, 'BUILD_ID'), 'utf8').trim()
168168

169-
const apiLambdas: APILambda[] = splitApiRoutes(featureFlags)
169+
const apiLambdas: APILambda[] = splitApiRoutes(featureFlags, publish)
170170
? await getAPILambdas(publish, appDir, pageExtensions)
171171
: await getExtendedApiRouteConfigs(publish, appDir, pageExtensions).then((extendedRoutes) =>
172172
extendedRoutes.map(packSingleFunction),
@@ -180,7 +180,7 @@ const plugin: NetlifyPlugin = {
180180
ignore,
181181
publish: relative(process.cwd(), publish),
182182
apiLambdas,
183-
splitApiRoutes: splitApiRoutes(featureFlags),
183+
splitApiRoutes: splitApiRoutes(featureFlags, publish),
184184
})
185185

186186
await movePublicFiles({ appDir, outdir, publish, basePath })

0 commit comments

Comments
 (0)