File tree 4 files changed +31
-8
lines changed
4 files changed +31
-8
lines changed Original file line number Diff line number Diff line change @@ -8,7 +8,6 @@ import slash from 'slash'
8
8
9
9
import { HANDLER_FUNCTION_NAME , IMAGE_FUNCTION_NAME , ODB_FUNCTION_NAME } from '../constants'
10
10
11
- import { splitApiRoutes } from './flags'
12
11
import type { APILambda } from './functions'
13
12
import type { RoutesManifest } from './types'
14
13
import { escapeStringRegexp } from './utils'
@@ -102,13 +101,13 @@ export const configureHandlerFunctions = async ({
102
101
publish,
103
102
ignore = [ ] ,
104
103
apiLambdas,
105
- featureFlags ,
104
+ splitApiRoutes ,
106
105
} : {
107
106
netlifyConfig : NetlifyConfig
108
107
publish : string
109
108
ignore : Array < string >
110
109
apiLambdas : APILambda [ ]
111
- featureFlags : Record < string , unknown >
110
+ splitApiRoutes : boolean
112
111
} ) => {
113
112
const config = await getRequiredServerFiles ( publish )
114
113
const files = config . files || [ ]
@@ -168,7 +167,7 @@ export const configureHandlerFunctions = async ({
168
167
configureFunction ( HANDLER_FUNCTION_NAME )
169
168
configureFunction ( ODB_FUNCTION_NAME )
170
169
171
- if ( splitApiRoutes ( featureFlags ) ) {
170
+ if ( splitApiRoutes ) {
172
171
for ( const apiLambda of apiLambdas ) {
173
172
const { functionName, includedFiles } = apiLambda
174
173
netlifyConfig . functions [ functionName ] ||= { included_files : [ ] }
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 or newer.' ,
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
- featureFlags,
183
+ splitApiRoutes : splitApiRoutes ( featureFlags , publish ) ,
184
184
} )
185
185
186
186
await movePublicFiles ( { appDir, outdir, publish, basePath } )
Original file line number Diff line number Diff line change @@ -727,6 +727,16 @@ describe('onBuild()', () => {
727
727
expect ( existsSync ( publicFile ) ) . toBe ( true )
728
728
expect ( await readJson ( publicFile ) ) . toMatchObject ( expect . any ( Array ) )
729
729
} )
730
+
731
+ it ( 'does not split APIs when .nft.json files are unavailable' , async ( ) => {
732
+ await moveNextDist ( )
733
+
734
+ await unlink ( path . join ( process . cwd ( ) , '.next' , 'next-server.js.nft.json' ) )
735
+
736
+ await nextRuntime . onBuild ( defaultArgs )
737
+
738
+ expect ( netlifyConfig . functions [ '_api_*' ] . node_bundler ) . toEqual ( 'nft' )
739
+ } )
730
740
} )
731
741
732
742
describe ( 'onPostBuild' , ( ) => {
You can’t perform that action at this time.
0 commit comments