@@ -16,18 +16,20 @@ import { isBinaryContentType } from "./binary.js";
16
16
import { debug } from "./logger.js" ;
17
17
import type { PublicFiles } from "../build.js" ;
18
18
import { convertFrom , convertTo } from "./event-mapper.js" ;
19
- import { overrideReact } from "./require-hooks.js" ;
20
- import { WarmerEvent , WarmerResponse } from "./warmer-function.js" ;
19
+ import { overrideDefault , overrideReact } from "./require-hooks.js" ;
20
+ import type { WarmerEvent , WarmerResponse } from "./warmer-function.js" ;
21
+
22
+ const NEXT_DIR = path . join ( __dirname , ".next" ) ;
23
+ const OPEN_NEXT_DIR = path . join ( __dirname , ".open-next" ) ;
24
+ const NODE_MODULES_DIR = path . join ( __dirname , "node_modules" ) ;
25
+ debug ( { NEXT_DIR , OPEN_NEXT_DIR } ) ;
21
26
22
27
setNodeEnv ( ) ;
23
28
setNextjsServerWorkingDirectory ( ) ;
24
- const nextDir = path . join ( __dirname , ".next" ) ;
25
- const openNextDir = path . join ( __dirname , ".open-next" ) ;
26
- const config = loadConfig ( nextDir ) ;
29
+ const config = loadConfig ( NEXT_DIR ) ;
27
30
const htmlPages = loadHtmlPages ( ) ;
28
31
const publicAssets = loadPublicAssets ( ) ;
29
32
initializeNextjsRequireHooks ( config ) ;
30
- debug ( { nextDir } ) ;
31
33
32
34
// Generate a 6 letter unique server ID
33
35
const serverId = `server-${ generateUniqueId ( ) } ` ;
@@ -127,6 +129,8 @@ function setNextjsServerWorkingDirectory() {
127
129
128
130
function initializeNextjsRequireHooks ( config : any ) {
129
131
// WORKAROUND: Set `__NEXT_PRIVATE_PREBUNDLED_REACT` to use prebundled React — https://github.com/serverless-stack/open-next#workaround-set-__next_private_prebundled_react-to-use-prebundled-react
132
+ if ( ! isNextjsVersionAtLeast ( "13.1.3" ) ) return ;
133
+ overrideDefault ( ) ;
130
134
overrideReact ( config ) ;
131
135
}
132
136
@@ -141,7 +145,7 @@ function setNextjsPrebundledReact(req: IncomingMessage, config: any) {
141
145
}
142
146
143
147
// pages route => use node_modules React
144
- if ( getMaybePagePath ( req . url , nextDir , config . i18n ?. locales , false ) ) {
148
+ if ( getMaybePagePath ( req . url , NEXT_DIR , config . i18n ?. locales , false ) ) {
145
149
process . env . __NEXT_PRIVATE_PREBUNDLED_REACT = undefined ;
146
150
return ;
147
151
}
@@ -153,20 +157,6 @@ function setNextjsPrebundledReact(req: IncomingMessage, config: any) {
153
157
: "next" ;
154
158
}
155
159
156
- function loadHtmlPages ( ) {
157
- const filePath = path . join ( nextDir , "server" , "pages-manifest.json" ) ;
158
- const json = fs . readFileSync ( filePath , "utf-8" ) ;
159
- return Object . entries ( JSON . parse ( json ) )
160
- . filter ( ( [ _ , value ] ) => ( value as string ) . endsWith ( ".html" ) )
161
- . map ( ( [ key ] ) => key ) ;
162
- }
163
-
164
- function loadPublicAssets ( ) {
165
- const filePath = path . join ( openNextDir , "public-files.json" ) ;
166
- const json = fs . readFileSync ( filePath , "utf-8" ) ;
167
- return JSON . parse ( json ) as PublicFiles ;
168
- }
169
-
170
160
async function processRequest ( req : IncomingMessage , res : ServerResponse ) {
171
161
// @ts -ignore
172
162
// Next.js doesn't parse body if the property exists
@@ -207,3 +197,31 @@ function formatWarmerResponse(event: WarmerEvent) {
207
197
} , event . delay ) ;
208
198
} ) ;
209
199
}
200
+
201
+ function isNextjsVersionAtLeast ( required : `${number } .${number } .${number } `) {
202
+ const filePath = path . join ( NODE_MODULES_DIR , "next" , "package.json" ) ;
203
+ const json = fs . readFileSync ( filePath , "utf-8" ) ;
204
+ const version = JSON . parse ( json ) . version ;
205
+
206
+ const [ major , minor , patch ] = version . split ( "-" ) [ 0 ] . split ( "." ) . map ( Number ) ;
207
+ const [ reqMajor , reqMinor , reqPatch ] = required . split ( "." ) . map ( Number ) ;
208
+ return (
209
+ major > reqMajor ||
210
+ ( major === reqMajor && minor > reqMinor ) ||
211
+ ( major === reqMajor && minor === reqMinor && patch >= reqPatch )
212
+ ) ;
213
+ }
214
+
215
+ function loadHtmlPages ( ) {
216
+ const filePath = path . join ( NEXT_DIR , "server" , "pages-manifest.json" ) ;
217
+ const json = fs . readFileSync ( filePath , "utf-8" ) ;
218
+ return Object . entries ( JSON . parse ( json ) )
219
+ . filter ( ( [ _ , value ] ) => ( value as string ) . endsWith ( ".html" ) )
220
+ . map ( ( [ key ] ) => key ) ;
221
+ }
222
+
223
+ function loadPublicAssets ( ) {
224
+ const filePath = path . join ( OPEN_NEXT_DIR , "public-files.json" ) ;
225
+ const json = fs . readFileSync ( filePath , "utf-8" ) ;
226
+ return JSON . parse ( json ) as PublicFiles ;
227
+ }
0 commit comments