Skip to content

Commit

Permalink
fix: force static
Browse files Browse the repository at this point in the history
  • Loading branch information
ovflowd committed Nov 3, 2023
1 parent 365d5ff commit c05c8cb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
12 changes: 9 additions & 3 deletions app/[...locale]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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;
13 changes: 9 additions & 4 deletions app/[locale]/[...path]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
7 changes: 6 additions & 1 deletion app/not-found.tsx
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
8 changes: 7 additions & 1 deletion app/page.tsx
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit c05c8cb

Please sign in to comment.