From c05c8cb95b6b0ba1a796164065fc8087121a05cb Mon Sep 17 00:00:00 2001 From: Claudio Wunder Date: Fri, 3 Nov 2023 13:40:14 +0100 Subject: [PATCH] fix: force static --- app/[...locale]/page.tsx | 12 +++++++++--- app/[locale]/[...path]/page.tsx | 13 +++++++++---- app/not-found.tsx | 7 ++++++- app/page.tsx | 8 +++++++- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/app/[...locale]/page.tsx b/app/[...locale]/page.tsx index d465cdb9018e4..c5439e55a8313 100644 --- a/app/[...locale]/page.tsx +++ b/app/[...locale]/page.tsx @@ -2,6 +2,7 @@ import { notFound } from 'next/navigation'; import { unstable_setRequestLocale } from 'next-intl/server'; import type { FC } from 'react'; +import { ENABLE_STATIC_EXPORT } from '@/next.constants.mjs'; import getDynamicRouter from '@/next.dynamic.mjs'; import { availableLocaleCodes, defaultLocale } from '@/next.locales.mjs'; import Theme from '@/theme'; @@ -15,8 +16,13 @@ const pathname = dynamicRouter.getPathname([]); // This method is used to retrieve all native statically supported pages (SCR) that // we want to provide during build-time + allow fallback for dynamic pages during (ISR) -export const generateStaticParams = async () => - availableLocaleCodes.map(locale => ({ locale: [locale] })); +export const generateStaticParams = async () => { + if (!ENABLE_STATIC_EXPORT) { + return []; + } + + return availableLocaleCodes.map(locale => ({ locale: [locale] })); +}; const DynamicPage: FC<{ params: DynamicStaticPaths }> = async ({ params }) => { const { locale = [defaultLocale.code] } = params; @@ -58,6 +64,6 @@ const DynamicPage: FC<{ params: DynamicStaticPaths }> = async ({ params }) => { }; // Enforce that all these routes are compatible with SSR -export const dynamic = 'auto'; +export const dynamic = 'error'; export default DynamicPage; diff --git a/app/[locale]/[...path]/page.tsx b/app/[locale]/[...path]/page.tsx index d70cb8baa532b..e507d5061534c 100644 --- a/app/[locale]/[...path]/page.tsx +++ b/app/[locale]/[...path]/page.tsx @@ -2,6 +2,7 @@ import { notFound } from 'next/navigation'; import { unstable_setRequestLocale } from 'next-intl/server'; import type { FC } from 'react'; +import { ENABLE_STATIC_EXPORT } from '@/next.constants.mjs'; import getDynamicRouter from '@/next.dynamic.mjs'; import { availableLocaleCodes, defaultLocale } from '@/next.locales.mjs'; import Theme from '@/theme'; @@ -15,12 +16,16 @@ const dynamicRouter = await getDynamicRouter(); export const generateStaticParams = async () => { const paths: DynamicStaticPaths[] = []; + if (!ENABLE_STATIC_EXPORT) { + return []; + } + for (const locale of availableLocaleCodes) { const routesForLanguage = await dynamicRouter.getRoutesByLanguage(locale); - const mappedRoutesWithLocale = routesForLanguage.map(pathname => - dynamicRouter.mapPathToRoute(locale, pathname) - ); + const mappedRoutesWithLocale = routesForLanguage + .filter(pathname => pathname.replace('.', '').length) + .map(pathname => dynamicRouter.mapPathToRoute(locale, pathname)); paths.push(...mappedRoutesWithLocale); } @@ -68,6 +73,6 @@ const DynamicPage: FC<{ params: DynamicStaticPaths }> = async ({ params }) => { }; // Enforce that all these routes are compatible with SSR -export const dynamic = 'auto'; +export const dynamic = 'error'; export default DynamicPage; diff --git a/app/not-found.tsx b/app/not-found.tsx index c41c5bc9d99f1..ec163c4743a9c 100644 --- a/app/not-found.tsx +++ b/app/not-found.tsx @@ -1,8 +1,13 @@ import { useTranslations } from 'next-intl'; +import { unstable_setRequestLocale } from 'next-intl/server'; +import type { FC } from 'react'; +import { defaultLocale } from '@/next.locales.mjs'; import Theme from '@/theme'; -const NotFound = () => { +const NotFound: FC = () => { + unstable_setRequestLocale(defaultLocale.code); + const t = useTranslations(); return ( diff --git a/app/page.tsx b/app/page.tsx index 0b9435601260c..d3320f51b1588 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,10 +1,16 @@ import { redirect } from 'next/navigation'; +import { unstable_setRequestLocale } from 'next-intl/server'; import type { FC } from 'react'; import { defaultLocale } from '@/next.locales.mjs'; // Redirects to the Default Locale in case we're on full-static mode // i.e. there's no middleware for language detection -const RootPage: FC = () => redirect(`/${defaultLocale.code}`); +const RootPage: FC = () => { + // This is a stopage implementation of the language detection middleware + unstable_setRequestLocale(defaultLocale.code); + + return redirect(`/${defaultLocale.code}`); +}; export default RootPage;