Skip to content

fix dataRoutes omitting basePath #905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/happy-pandas-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@opennextjs/aws": patch
"tests-unit": patch
---

fix dataRoutes omitting basePath in page router (#897)
8 changes: 4 additions & 4 deletions packages/open-next/src/core/routing/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions packages/tests-unit/tests/core/routing/matcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading