Skip to content

Commit 1ece6b4

Browse files
alebelcorconico974
andauthored
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 <nicodorseuil@yahoo.fr>
1 parent 44392ba commit 1ece6b4

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

.changeset/thirty-grapes-punch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
fix(aws): add missing base path to data routes

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ export function fixDataPage(
336336
buildId: string,
337337
): InternalEvent | InternalResult {
338338
const { rawPath, query } = internalEvent;
339-
const dataPattern = `/_next/data/${buildId}`;
339+
const dataPattern = `${NextConfig.basePath ?? ""}/_next/data/${buildId}`;
340+
340341
// Return 404 for data requests that don't match the buildId
341342
if (rawPath.startsWith("/_next/data") && !rawPath.startsWith(dataPattern)) {
342343
return {

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,21 @@ describe("fixDataPage", () => {
490490
expect(response).toEqual(event);
491491
});
492492

493+
it("should not return 404 for data requests (with base path) that don't match the buildId", () => {
494+
NextConfig.basePath = "/base";
495+
496+
const event = createEvent({
497+
url: "/base/_next/data/abc/test",
498+
});
499+
500+
const response = fixDataPage(event, "abc");
501+
502+
expect(response.statusCode).not.toEqual(404);
503+
expect(response).toEqual(event);
504+
505+
NextConfig.basePath = undefined;
506+
});
507+
493508
it("should remove json extension from data requests and add __nextDataReq to query", () => {
494509
const event = createEvent({
495510
url: "/_next/data/abc/test/file.json?hello=world",
@@ -503,4 +518,22 @@ describe("fixDataPage", () => {
503518
url: "/test/file?hello=world&__nextDataReq=1",
504519
});
505520
});
521+
522+
it("should remove json extension from data requests (with base path) and add __nextDataReq to query", () => {
523+
NextConfig.basePath = "/base";
524+
525+
const event = createEvent({
526+
url: "/base/_next/data/abc/test/file.json?hello=world",
527+
});
528+
529+
const response = fixDataPage(event, "abc");
530+
531+
expect(response).toEqual({
532+
...event,
533+
rawPath: "/test/file",
534+
url: "/test/file?hello=world&__nextDataReq=1",
535+
});
536+
537+
NextConfig.basePath = undefined;
538+
});
506539
});

0 commit comments

Comments
 (0)