Skip to content

Commit

Permalink
fix(aws): add missing base path to data routes (#672)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Dec 11, 2024
1 parent 44392ba commit 1ece6b4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/thirty-grapes-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

fix(aws): add missing base path to data routes
3 changes: 2 additions & 1 deletion packages/open-next/src/core/routing/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ export function fixDataPage(
buildId: string,
): InternalEvent | InternalResult {
const { rawPath, query } = internalEvent;
const dataPattern = `/_next/data/${buildId}`;
const dataPattern = `${NextConfig.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 Down
33 changes: 33 additions & 0 deletions packages/tests-unit/tests/core/routing/matcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,21 @@ describe("fixDataPage", () => {
expect(response).toEqual(event);
});

it("should not return 404 for data requests (with base path) that don't match the buildId", () => {
NextConfig.basePath = "/base";

const event = createEvent({
url: "/base/_next/data/abc/test",
});

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

expect(response.statusCode).not.toEqual(404);
expect(response).toEqual(event);

NextConfig.basePath = undefined;
});

it("should remove json extension from data requests and add __nextDataReq to query", () => {
const event = createEvent({
url: "/_next/data/abc/test/file.json?hello=world",
Expand All @@ -503,4 +518,22 @@ describe("fixDataPage", () => {
url: "/test/file?hello=world&__nextDataReq=1",
});
});

it("should remove json extension from data requests (with base path) and add __nextDataReq to query", () => {
NextConfig.basePath = "/base";

const event = createEvent({
url: "/base/_next/data/abc/test/file.json?hello=world",
});

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

expect(response).toEqual({
...event,
rawPath: "/test/file",
url: "/test/file?hello=world&__nextDataReq=1",
});

NextConfig.basePath = undefined;
});
});

0 comments on commit 1ece6b4

Please sign in to comment.