File tree Expand file tree Collapse file tree 2 files changed +18
-4
lines changed Expand file tree Collapse file tree 2 files changed +18
-4
lines changed Original file line number Diff line number Diff line change 1
1
import destr from 'destr'
2
+ import { existsSync } from 'fs-extra'
3
+ import { join } from 'pathe'
2
4
3
5
/**
4
6
* If this flag is enabled, we generate individual Lambda functions for API Routes.
@@ -11,7 +13,19 @@ import destr from 'destr'
11
13
* If disabled, we bundle all API Routes into a single function.
12
14
* This is can lead to large bundle sizes.
13
15
*
16
+ * Relies on `next-server.js.nft.json`, which is only supported in Next.js 12+.
17
+ *
14
18
* Disabled by default. Can be overriden using the NEXT_SPLIT_API_ROUTES env var.
15
19
*/
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
+ }
Original file line number Diff line number Diff line change @@ -166,7 +166,7 @@ const plugin: NetlifyPlugin = {
166
166
167
167
const buildId = readFileSync ( join ( publish , 'BUILD_ID' ) , 'utf8' ) . trim ( )
168
168
169
- const apiLambdas : APILambda [ ] = splitApiRoutes ( featureFlags )
169
+ const apiLambdas : APILambda [ ] = splitApiRoutes ( featureFlags , publish )
170
170
? await getAPILambdas ( publish , appDir , pageExtensions )
171
171
: await getExtendedApiRouteConfigs ( publish , appDir , pageExtensions ) . then ( ( extendedRoutes ) =>
172
172
extendedRoutes . map ( packSingleFunction ) ,
@@ -180,7 +180,7 @@ const plugin: NetlifyPlugin = {
180
180
ignore,
181
181
publish : relative ( process . cwd ( ) , publish ) ,
182
182
apiLambdas,
183
- splitApiRoutes : splitApiRoutes ( featureFlags ) ,
183
+ splitApiRoutes : splitApiRoutes ( featureFlags , publish ) ,
184
184
} )
185
185
186
186
await movePublicFiles ( { appDir, outdir, publish, basePath } )
You can’t perform that action at this time.
0 commit comments