diff --git a/.changeset/thirty-grapes-punch.md b/.changeset/thirty-grapes-punch.md new file mode 100644 index 000000000..fb8746e29 --- /dev/null +++ b/.changeset/thirty-grapes-punch.md @@ -0,0 +1,5 @@ +--- +"@opennextjs/aws": patch +--- + +fix(aws): add missing base path to data routes diff --git a/packages/open-next/src/core/routing/matcher.ts b/packages/open-next/src/core/routing/matcher.ts index 032a3a3be..8c0dabad0 100644 --- a/packages/open-next/src/core/routing/matcher.ts +++ b/packages/open-next/src/core/routing/matcher.ts @@ -336,7 +336,8 @@ export function fixDataPage( buildId: string, ): InternalEvent | InternalResult { const { rawPath, query } = internalEvent; - const dataPattern = `/_next/data/${buildId}`; + const dataPattern = `${NextConfig.basePath ?? ""}/_next/data/${buildId}`; + // Return 404 for data requests that don't match the buildId if (rawPath.startsWith("/_next/data") && !rawPath.startsWith(dataPattern)) { return { diff --git a/packages/tests-unit/tests/core/routing/matcher.test.ts b/packages/tests-unit/tests/core/routing/matcher.test.ts index 96ff0c87e..c44a3ffc0 100644 --- a/packages/tests-unit/tests/core/routing/matcher.test.ts +++ b/packages/tests-unit/tests/core/routing/matcher.test.ts @@ -490,6 +490,21 @@ describe("fixDataPage", () => { expect(response).toEqual(event); }); + it("should not return 404 for data requests (with base path) that don't match the buildId", () => { + NextConfig.basePath = "/base"; + + const event = createEvent({ + url: "/base/_next/data/abc/test", + }); + + const response = fixDataPage(event, "abc"); + + expect(response.statusCode).not.toEqual(404); + expect(response).toEqual(event); + + NextConfig.basePath = undefined; + }); + it("should remove json extension from data requests and add __nextDataReq to query", () => { const event = createEvent({ url: "/_next/data/abc/test/file.json?hello=world", @@ -503,4 +518,22 @@ describe("fixDataPage", () => { url: "/test/file?hello=world&__nextDataReq=1", }); }); + + it("should remove json extension from data requests (with base path) and add __nextDataReq to query", () => { + NextConfig.basePath = "/base"; + + const event = createEvent({ + url: "/base/_next/data/abc/test/file.json?hello=world", + }); + + const response = fixDataPage(event, "abc"); + + expect(response).toEqual({ + ...event, + rawPath: "/test/file", + url: "/test/file?hello=world&__nextDataReq=1", + }); + + NextConfig.basePath = undefined; + }); });