Skip to content
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