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: normalize the rawPath without locale before attempting to asserting the 404 page #448

Merged
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
5 changes: 5 additions & 0 deletions .changeset/tidy-mice-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"open-next": patch
---

fix 404 handeling with i18n routes
7 changes: 6 additions & 1 deletion examples/pages-router/next.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
transpilePackages: ["@example/shared"],
i18n: {
locales: ["en", "nl"],
defaultLocale: "en",
},
cleanDistDir: true,
reactStrictMode: true,
output: "standalone",
Expand All @@ -9,10 +13,11 @@ const nextConfig = {
ignoreDuringBuilds: true,
},
rewrites: () => [
{ source: "/rewrite", destination: "/" },
{ source: "/rewrite", destination: "/", locale: false },
{
source: "/rewriteUsingQuery",
destination: "/:destination/",
locale: false,
has: [
{
type: "query",
Expand Down
1 change: 1 addition & 0 deletions packages/open-next/src/adapters/config/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function loadRoutesManifest(nextDir: string) {
dynamic: routesManifest.dynamicRoutes ?? [],
data: dataRoutes,
},
locales: routesManifest.i18n?.locales ?? [],
};
}

Expand Down
10 changes: 8 additions & 2 deletions packages/open-next/src/core/routingHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ export interface MiddlewareOutputEvent {
origin: Origin | false;
}

// Add the locale prefix to the regex so we correctly match the rawPath
const optionalLocalePrefixRegex = !!RoutesManifest.locales.length
? `^/(?:${RoutesManifest.locales.map((locale) => `${locale}/?`).join("|")})?`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cause an issue with 3 e2e test because of this, it should also include the default case (i.e. with no locales prefix)
image
For the next data test it is because of this https://github.com/sst/open-next/blob/9d632d1bd5c3a8d7e7bbd13ae9daaca68da9284e/packages/tests-e2e/tests/pagesRouter/data.test.ts#L13 It ends now with en.json

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the best approach here? Create a new pages app with i18n + additional e2e tests? 🤔 . It's expected that in this case it's en.json instead of index.json (but only if you have i18n settings).

It's a bit of a mess (on Next.js side)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a perfect world, yeah we should do something like that, but honestly just testing with en.json is probably enough

: "^/";

const staticRegexp = RoutesManifest.routes.static.map(
(route) => new RegExp(route.regex),
(route) => new RegExp(route.regex.replace("^/", optionalLocalePrefixRegex)),
);

const dynamicRegexp = RoutesManifest.routes.dynamic.map(
(route) => new RegExp(route.regex),
(route) => new RegExp(route.regex.replace("^/", optionalLocalePrefixRegex)),
);

export default async function routingHandler(
Expand Down Expand Up @@ -67,6 +72,7 @@ export default async function routingHandler(
internalEvent = beforeRewrites.internalEvent;
isExternalRewrite = beforeRewrites.isExternalRewrite;
}

const isStaticRoute =
!isExternalRewrite &&
staticRegexp.some((route) =>
Expand Down
3 changes: 3 additions & 0 deletions packages/open-next/src/types/next-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ export interface RoutesManifest {
| RewriteDefinition[];
redirects: RedirectDefinition[];
headers?: Header[];
i18n?: {
locales: string[];
};
}

export interface MiddlewareInfo {
Expand Down
8 changes: 4 additions & 4 deletions packages/tests-e2e/tests/pagesRouter/data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import { expect, test } from "@playwright/test";
test("fix _next/data", async ({ page }) => {
await page.goto("/");

const isrJson = page.waitForResponse("/_next/data/*/isr.json");
const isrJson = page.waitForResponse("/_next/data/*/en/isr.json");
await page.locator('[href="/isr/"]').click();
const response = await isrJson;
expect(response.ok()).toBe(true);
expect(response.request().url()).toMatch(/\/_next\/data\/.*\/isr\.json$/);
expect(response.request().url()).toMatch(/\/_next\/data\/.*\/en\/isr\.json$/);
await page.waitForURL("/isr/");

const homeJson = page.waitForResponse("/_next/data/*/index.json");
const homeJson = page.waitForResponse("/_next/data/*/en.json");
await page.locator('[href="/"]').click();
const response2 = await homeJson;
expect(response2.ok()).toBe(true);
expect(response2.request().url()).toMatch(/\/_next\/data\/.*\/index\.json$/);
expect(response2.request().url()).toMatch(/\/_next\/data\/.*\/en\.json$/);
await page.waitForURL("/");
const body = await response2.json();
expect(body).toEqual({ pageProps: { hello: "world" }, __N_SSG: true });
Expand Down
Loading