From 1ece6b479bb4e0309892ffbd1200870821a410c4 Mon Sep 17 00:00:00 2001 From: Alejandro Date: Wed, 11 Dec 2024 14:58:19 -0700 Subject: [PATCH] fix(aws): add missing base path to data routes (#672) * fix(aws): add missing base path to data routes * chore(aws): add tests for fixDataPage when base path is used * chore(aws): remove unnecessary cleanup * chore(aws): format with biome * chore(aws): restore cleanup * chore(aws): format with biome * Create thirty-grapes-punch.md --------- Co-authored-by: alebelcor <856838+alebelcor@users.noreply.github.com> Co-authored-by: conico974 --- .changeset/thirty-grapes-punch.md | 5 +++ .../open-next/src/core/routing/matcher.ts | 3 +- .../tests/core/routing/matcher.test.ts | 33 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 .changeset/thirty-grapes-punch.md diff --git a/.changeset/thirty-grapes-punch.md b/.changeset/thirty-grapes-punch.md new file mode 100644 index 00000000..fb8746e2 --- /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 032a3a3b..8c0dabad 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 96ff0c87..c44a3ffc 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; + }); });