Skip to content

Commit f46bc00

Browse files
fix dataRoutes omitting basePath (#905)
* fix dataRoutes omitting basePath (#897) * Create happy-pandas-hope.md --------- Co-authored-by: conico974 <[email protected]>
1 parent 075be1e commit f46bc00

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

.changeset/happy-pandas-hope.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@opennextjs/aws": patch
3+
"tests-unit": patch
4+
---
5+
6+
fix dataRoutes omitting basePath in page router (#897)

packages/open-next/src/core/routing/matcher.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ export function fixDataPage(
381381
buildId: string,
382382
): InternalEvent | InternalResult {
383383
const { rawPath, query } = internalEvent;
384-
const dataPattern = `${NextConfig.basePath ?? ""}/_next/data/${buildId}`;
385-
384+
const basePath = NextConfig.basePath ?? "";
385+
const dataPattern = `${basePath}/_next/data/${buildId}`;
386386
// Return 404 for data requests that don't match the buildId
387387
if (rawPath.startsWith("/_next/data") && !rawPath.startsWith(dataPattern)) {
388388
return {
@@ -397,9 +397,9 @@ export function fixDataPage(
397397
}
398398

399399
if (rawPath.startsWith(dataPattern) && rawPath.endsWith(".json")) {
400-
const newPath = rawPath
400+
const newPath = `${basePath}${rawPath
401401
.slice(dataPattern.length, -".json".length)
402-
.replace(/^\/index$/, "/");
402+
.replace(/^\/index$/, "/")}`;
403403
query.__nextDataReq = "1";
404404

405405
return {

packages/tests-unit/tests/core/routing/matcher.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,18 +598,19 @@ describe("fixDataPage", () => {
598598
});
599599

600600
it("should remove json extension from data requests (with base path) and add __nextDataReq to query", () => {
601-
NextConfig.basePath = "/base";
601+
const mockBasePath = "/base";
602+
NextConfig.basePath = mockBasePath;
602603

603604
const event = createEvent({
604-
url: "https://on/base/_next/data/abc/test/file.json?hello=world",
605+
url: `https://on${mockBasePath}/_next/data/abc/test/file.json?hello=world`,
605606
});
606607

607608
const response = fixDataPage(event, "abc");
608609

609610
expect(response).toEqual({
610611
...event,
611-
rawPath: "/test/file",
612-
url: "https://on/test/file?hello=world&__nextDataReq=1",
612+
rawPath: `${mockBasePath}/test/file`,
613+
url: `https://on${mockBasePath}/test/file?hello=world&__nextDataReq=1`,
613614
});
614615

615616
NextConfig.basePath = undefined;

0 commit comments

Comments
 (0)