Skip to content

Commit 7931bee

Browse files
fix: normalize the rawPath without locale before attempting to asserting the 404 page (#448)
* fix: normalize the rawPath without locale before attempting to assert the 404 page * fix: eslint issues * fix: refactor to enhance existing regex * chore: add changelog * fix: ensure root locale urls are captured * chore: add i18n configuration * fix: use double quotes * fix e2e --------- Co-authored-by: Dorseuil Nicolas <[email protected]>
1 parent 208f7ba commit 7931bee

File tree

6 files changed

+27
-7
lines changed

6 files changed

+27
-7
lines changed

.changeset/tidy-mice-train.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"open-next": patch
3+
---
4+
5+
fix 404 handeling with i18n routes

examples/pages-router/next.config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
/** @type {import('next').NextConfig} */
22
const nextConfig = {
33
transpilePackages: ["@example/shared"],
4+
i18n: {
5+
locales: ["en", "nl"],
6+
defaultLocale: "en",
7+
},
48
cleanDistDir: true,
59
reactStrictMode: true,
610
output: "standalone",
@@ -9,10 +13,11 @@ const nextConfig = {
913
ignoreDuringBuilds: true,
1014
},
1115
rewrites: () => [
12-
{ source: "/rewrite", destination: "/" },
16+
{ source: "/rewrite", destination: "/", locale: false },
1317
{
1418
source: "/rewriteUsingQuery",
1519
destination: "/:destination/",
20+
locale: false,
1621
has: [
1722
{
1823
type: "query",

packages/open-next/src/adapters/config/util.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export function loadRoutesManifest(nextDir: string) {
5959
dynamic: routesManifest.dynamicRoutes ?? [],
6060
data: dataRoutes,
6161
},
62+
locales: routesManifest.i18n?.locales ?? [],
6263
};
6364
}
6465

packages/open-next/src/core/routingHandler.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ export interface MiddlewareOutputEvent {
2222
origin: Origin | false;
2323
}
2424

25+
// Add the locale prefix to the regex so we correctly match the rawPath
26+
const optionalLocalePrefixRegex = !!RoutesManifest.locales.length
27+
? `^/(?:${RoutesManifest.locales.map((locale) => `${locale}/?`).join("|")})?`
28+
: "^/";
29+
2530
const staticRegexp = RoutesManifest.routes.static.map(
26-
(route) => new RegExp(route.regex),
31+
(route) => new RegExp(route.regex.replace("^/", optionalLocalePrefixRegex)),
2732
);
2833

2934
const dynamicRegexp = RoutesManifest.routes.dynamic.map(
30-
(route) => new RegExp(route.regex),
35+
(route) => new RegExp(route.regex.replace("^/", optionalLocalePrefixRegex)),
3136
);
3237

3338
export default async function routingHandler(
@@ -67,6 +72,7 @@ export default async function routingHandler(
6772
internalEvent = beforeRewrites.internalEvent;
6873
isExternalRewrite = beforeRewrites.isExternalRewrite;
6974
}
75+
7076
const isStaticRoute =
7177
!isExternalRewrite &&
7278
staticRegexp.some((route) =>

packages/open-next/src/types/next-types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ export interface RoutesManifest {
112112
| RewriteDefinition[];
113113
redirects: RedirectDefinition[];
114114
headers?: Header[];
115+
i18n?: {
116+
locales: string[];
117+
};
115118
}
116119

117120
export interface MiddlewareInfo {

packages/tests-e2e/tests/pagesRouter/data.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ import { expect, test } from "@playwright/test";
33
test("fix _next/data", async ({ page }) => {
44
await page.goto("/");
55

6-
const isrJson = page.waitForResponse("/_next/data/*/isr.json");
6+
const isrJson = page.waitForResponse("/_next/data/*/en/isr.json");
77
await page.locator('[href="/isr/"]').click();
88
const response = await isrJson;
99
expect(response.ok()).toBe(true);
10-
expect(response.request().url()).toMatch(/\/_next\/data\/.*\/isr\.json$/);
10+
expect(response.request().url()).toMatch(/\/_next\/data\/.*\/en\/isr\.json$/);
1111
await page.waitForURL("/isr/");
1212

13-
const homeJson = page.waitForResponse("/_next/data/*/index.json");
13+
const homeJson = page.waitForResponse("/_next/data/*/en.json");
1414
await page.locator('[href="/"]').click();
1515
const response2 = await homeJson;
1616
expect(response2.ok()).toBe(true);
17-
expect(response2.request().url()).toMatch(/\/_next\/data\/.*\/index\.json$/);
17+
expect(response2.request().url()).toMatch(/\/_next\/data\/.*\/en\.json$/);
1818
await page.waitForURL("/");
1919
const body = await response2.json();
2020
expect(body).toEqual({ pageProps: { hello: "world" }, __N_SSG: true });

0 commit comments

Comments
 (0)