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

fix(aws): add missing base path to data routes #672

Merged
Merged
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;
});
});