Skip to content

Commit 5c80192

Browse files
authored
Fix incorrect 200 with wrong buildId (#397)
* fix incorrect 200 with wrong buildId * Create blue-guests-punch.md
1 parent 2118ba2 commit 5c80192

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

.changeset/blue-guests-punch.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"open-next": patch
3+
---
4+
5+
Fix incorrect 200 with wrong buildId for page router

packages/open-next/src/adapters/plugins/routing/default.replacement.ts

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ export const processInternalEvent: ProcessInternalEvent = async (
4242
const nextHeaders = addNextConfigHeaders(event, ConfigHeaders) ?? {};
4343

4444
let internalEvent = fixDataPage(event, BuildId);
45+
// If we return InternalResult, it means that the build id is not correct
46+
// We should return a 404
47+
if ("statusCode" in internalEvent) {
48+
return internalEvent;
49+
}
4550

4651
internalEvent = handleFallbackFalse(internalEvent, PrerenderManifest);
4752

packages/open-next/src/adapters/routing/matcher.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,24 @@ export function handleRedirects(
239239
}
240240
}
241241

242-
export function fixDataPage(internalEvent: InternalEvent, buildId: string) {
242+
export function fixDataPage(
243+
internalEvent: InternalEvent,
244+
buildId: string,
245+
): InternalEvent | InternalResult {
243246
const { rawPath, query } = internalEvent;
244247
const dataPattern = `/_next/data/${buildId}`;
248+
// Return 404 for data requests that don't match the buildId
249+
if (rawPath.startsWith("/_next/data") && !rawPath.startsWith(dataPattern)) {
250+
return {
251+
type: internalEvent.type,
252+
statusCode: 404,
253+
body: "{}",
254+
headers: {
255+
"Content-Type": "application/json",
256+
},
257+
isBase64Encoded: false,
258+
};
259+
}
245260

246261
if (rawPath.startsWith(dataPattern) && rawPath.endsWith(".json")) {
247262
let newPath = rawPath.replace(dataPattern, "").replace(/\.json$/, "");

0 commit comments

Comments
 (0)