Skip to content

Commit f57eca1

Browse files
fix: redirect to default locale page when locale is not supported (nodejs#6628)
1 parent 62274cb commit f57eca1

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

app/[locale]/[[...path]]/page.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { setContext, setTags } from '@sentry/nextjs';
2-
import { notFound } from 'next/navigation';
2+
import { notFound, redirect } from 'next/navigation';
33
import { unstable_setRequestLocale } from 'next-intl/server';
44
import type { FC } from 'react';
55

@@ -9,7 +9,11 @@ import WithLayout from '@/components/withLayout';
99
import { ENABLE_STATIC_EXPORT, VERCEL_REVALIDATE } from '@/next.constants.mjs';
1010
import { PAGE_VIEWPORT, DYNAMIC_ROUTES } from '@/next.dynamic.constants.mjs';
1111
import { dynamicRouter } from '@/next.dynamic.mjs';
12-
import { availableLocaleCodes, defaultLocale } from '@/next.locales.mjs';
12+
import {
13+
allLocaleCodes,
14+
availableLocaleCodes,
15+
defaultLocale,
16+
} from '@/next.locales.mjs';
1317
import { MatterProvider } from '@/providers/matterProvider';
1418

1519
type DynamicStaticPaths = { path: Array<string>; locale: string };
@@ -67,7 +71,14 @@ const getPage: FC<DynamicParams> = async ({ params }) => {
6771
// Forces the current locale to be the Default Locale
6872
unstable_setRequestLocale(defaultLocale.code);
6973

70-
return notFound();
74+
if (!allLocaleCodes.includes(locale)) {
75+
// when the locale is not listed in the locales, return NotFound
76+
return notFound();
77+
}
78+
79+
// Redirect to the default locale path
80+
const pathname = dynamicRouter.getPathname(path);
81+
return redirect(`/${defaultLocale.code}/${pathname}`);
7182
}
7283

7384
// Configures the current Locale to be the given Locale of the Request

next.locales.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ const availableLocalesMap = Object.fromEntries(
2020
localeConfig.map(locale => [locale.code, locale])
2121
);
2222

23+
// Creates all supported locales
24+
const allLocaleCodes = localeConfig.map(locale => locale.code);
25+
2326
export {
27+
allLocaleCodes,
2428
availableLocales,
2529
availableLocaleCodes,
2630
availableLocalesMap,

0 commit comments

Comments
 (0)