@@ -14,11 +14,14 @@ import NextServer from "next/dist/server/next-server.js";
14
14
import { loadConfig } from "./util.js" ;
15
15
import { isBinaryContentType } from "./binary.js" ;
16
16
import { debug } from "./logger.js" ;
17
+ import type { PublicAssets } from "../build.js" ;
17
18
18
19
setNextjsServerWorkingDirectory ( ) ;
19
20
const nextDir = path . join ( __dirname , ".next" ) ;
21
+ const openNextDir = path . join ( __dirname , ".open-next" ) ;
20
22
const config = loadConfig ( nextDir ) ;
21
23
const htmlPages = loadHtmlPages ( ) ;
24
+ const publicAssets = loadPublicAssets ( ) ;
22
25
debug ( { nextDir } ) ;
23
26
24
27
// Create a NextServer
@@ -132,6 +135,18 @@ export async function handler(
132
135
const parser = isCloudFrontEvent
133
136
? eventParser . cloudfront ( event as CloudFrontRequestEvent )
134
137
: eventParser . apiv2 ( event as APIGatewayProxyEventV2 ) ;
138
+
139
+ // WORKAROUND: public/ static files served by the server function (AWS specific) — https://github.com/serverless-stack/open-next#workaround-public-static-files-served-by-the-server-function-aws-specific
140
+ if (
141
+ publicAssets [ parser . rawPath ] === "file" ||
142
+ publicAssets [ parser . rawPath . split ( "/" ) [ 0 ] ] === "dir"
143
+ ) {
144
+ return isCloudFrontEvent
145
+ ? formatCloudFrontFailoverResponse ( event as CloudFrontRequestEvent )
146
+ : formatApiv2FailoverResponse ( ) ;
147
+ }
148
+
149
+ // Process Next.js request
135
150
const reqProps = {
136
151
method : parser . method ,
137
152
url : parser . url ,
@@ -142,8 +157,6 @@ export async function handler(
142
157
debug ( "IncomingMessage constructor props" , reqProps ) ;
143
158
const req = new IncomingMessage ( reqProps ) ;
144
159
const res = new ServerResponse ( { method : reqProps . method } ) ;
145
-
146
- // Process Next.js request
147
160
await processRequest ( req , res ) ;
148
161
149
162
// Format Next.js response to Lambda response
@@ -165,10 +178,7 @@ export async function handler(
165
178
}
166
179
167
180
return isCloudFrontEvent
168
- ? // WORKAROUND: public/ static files served by the server function (AWS specific) — https://github.com/serverless-stack/open-next#workaround-public-static-files-served-by-the-server-function-aws-specific
169
- statusCode === 404
170
- ? formatCloudFrontFailoverResponse ( event as CloudFrontRequestEvent )
171
- : formatCloudFrontResponse ( { statusCode, headers, isBase64Encoded, body } )
181
+ ? formatCloudFrontResponse ( { statusCode, headers, isBase64Encoded, body } )
172
182
: formatApiv2Response ( { statusCode, headers, isBase64Encoded, body } ) ;
173
183
}
174
184
@@ -189,6 +199,12 @@ function loadHtmlPages() {
189
199
. map ( ( [ key ] ) => key ) ;
190
200
}
191
201
202
+ function loadPublicAssets ( ) {
203
+ const filePath = path . join ( openNextDir , "public-files.json" ) ;
204
+ const json = fs . readFileSync ( filePath , "utf-8" ) ;
205
+ return JSON . parse ( json ) as PublicAssets ;
206
+ }
207
+
192
208
async function processRequest ( req : IncomingMessage , res : ServerResponse ) {
193
209
// @ts -ignore
194
210
// Next.js doesn't parse body if the property exists
@@ -246,6 +262,10 @@ function formatApiv2Response({
246
262
return response ;
247
263
}
248
264
265
+ function formatApiv2FailoverResponse ( ) {
266
+ return { statusCode : 503 } ;
267
+ }
268
+
249
269
function formatCloudFrontResponse ( {
250
270
statusCode,
251
271
headers : rawHeaders ,
0 commit comments