From 29feb7e7ca49afd027881f925c3cbbcc21b87416 Mon Sep 17 00:00:00 2001 From: Kai Ming Chia Date: Fri, 13 Jun 2025 17:59:51 +0800 Subject: [PATCH 1/2] fix dataRoutes omitting basePath (#897) --- packages/open-next/src/core/routing/matcher.ts | 8 ++++---- packages/tests-unit/tests/core/routing/matcher.test.ts | 9 +++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/open-next/src/core/routing/matcher.ts b/packages/open-next/src/core/routing/matcher.ts index d71f7d8d6..48c005944 100644 --- a/packages/open-next/src/core/routing/matcher.ts +++ b/packages/open-next/src/core/routing/matcher.ts @@ -381,8 +381,8 @@ export function fixDataPage( buildId: string, ): InternalEvent | InternalResult { const { rawPath, query } = internalEvent; - const dataPattern = `${NextConfig.basePath ?? ""}/_next/data/${buildId}`; - + const basePath = NextConfig.basePath ?? ""; + const dataPattern = `${basePath}/_next/data/${buildId}`; // Return 404 for data requests that don't match the buildId if (rawPath.startsWith("/_next/data") && !rawPath.startsWith(dataPattern)) { return { @@ -397,9 +397,9 @@ export function fixDataPage( } if (rawPath.startsWith(dataPattern) && rawPath.endsWith(".json")) { - const newPath = rawPath + const newPath = `${basePath}${rawPath .slice(dataPattern.length, -".json".length) - .replace(/^\/index$/, "/"); + .replace(/^\/index$/, "/")}`; query.__nextDataReq = "1"; return { diff --git a/packages/tests-unit/tests/core/routing/matcher.test.ts b/packages/tests-unit/tests/core/routing/matcher.test.ts index 4b1f5383e..c5afcd8de 100644 --- a/packages/tests-unit/tests/core/routing/matcher.test.ts +++ b/packages/tests-unit/tests/core/routing/matcher.test.ts @@ -598,18 +598,19 @@ describe("fixDataPage", () => { }); it("should remove json extension from data requests (with base path) and add __nextDataReq to query", () => { - NextConfig.basePath = "/base"; + const mockBasePath = "/base"; + NextConfig.basePath = mockBasePath; const event = createEvent({ - url: "https://on/base/_next/data/abc/test/file.json?hello=world", + url: `https://on${mockBasePath}/_next/data/abc/test/file.json?hello=world`, }); const response = fixDataPage(event, "abc"); expect(response).toEqual({ ...event, - rawPath: "/test/file", - url: "https://on/test/file?hello=world&__nextDataReq=1", + rawPath: `${mockBasePath}/test/file`, + url: `https://on${mockBasePath}/test/file?hello=world&__nextDataReq=1`, }); NextConfig.basePath = undefined; From 77d155eaa5467272f13165a6ec42923389e9252b Mon Sep 17 00:00:00 2001 From: conico974 Date: Tue, 17 Jun 2025 18:16:03 +0200 Subject: [PATCH 2/2] Create happy-pandas-hope.md --- .changeset/happy-pandas-hope.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/happy-pandas-hope.md diff --git a/.changeset/happy-pandas-hope.md b/.changeset/happy-pandas-hope.md new file mode 100644 index 000000000..c0cd93beb --- /dev/null +++ b/.changeset/happy-pandas-hope.md @@ -0,0 +1,6 @@ +--- +"@opennextjs/aws": patch +"tests-unit": patch +--- + +fix dataRoutes omitting basePath in page router (#897)