Skip to content

Commit

Permalink
fix: redirect to default locale page when locale is not supported (no…
Browse files Browse the repository at this point in the history
  • Loading branch information
yosuke-furukawa committed Apr 18, 2024
1 parent 62274cb commit f57eca1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
17 changes: 14 additions & 3 deletions app/[locale]/[[...path]]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { setContext, setTags } from '@sentry/nextjs';
import { notFound } from 'next/navigation';
import { notFound, redirect } from 'next/navigation';
import { unstable_setRequestLocale } from 'next-intl/server';
import type { FC } from 'react';

Expand All @@ -9,7 +9,11 @@ import WithLayout from '@/components/withLayout';
import { ENABLE_STATIC_EXPORT, VERCEL_REVALIDATE } from '@/next.constants.mjs';
import { PAGE_VIEWPORT, DYNAMIC_ROUTES } from '@/next.dynamic.constants.mjs';
import { dynamicRouter } from '@/next.dynamic.mjs';
import { availableLocaleCodes, defaultLocale } from '@/next.locales.mjs';
import {
allLocaleCodes,
availableLocaleCodes,
defaultLocale,
} from '@/next.locales.mjs';
import { MatterProvider } from '@/providers/matterProvider';

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

return notFound();
if (!allLocaleCodes.includes(locale)) {
// when the locale is not listed in the locales, return NotFound
return notFound();
}

// Redirect to the default locale path
const pathname = dynamicRouter.getPathname(path);
return redirect(`/${defaultLocale.code}/${pathname}`);
}

// Configures the current Locale to be the given Locale of the Request
Expand Down
4 changes: 4 additions & 0 deletions next.locales.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ const availableLocalesMap = Object.fromEntries(
localeConfig.map(locale => [locale.code, locale])
);

// Creates all supported locales
const allLocaleCodes = localeConfig.map(locale => locale.code);

export {
allLocaleCodes,
availableLocales,
availableLocaleCodes,
availableLocalesMap,
Expand Down

0 comments on commit f57eca1

Please sign in to comment.