Skip to content

Commit

Permalink
update nextjs
Browse files Browse the repository at this point in the history
  • Loading branch information
AugustinMauroy committed Nov 28, 2024
1 parent ab582ff commit 2d90748
Show file tree
Hide file tree
Showing 20 changed files with 243 additions and 238 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
enable-pre-post-scripts=true
legacy-peer-deps=true
13 changes: 6 additions & 7 deletions app/[locale]/404/page.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { ArrowRightIcon } from '@heroicons/react/24/outline';
import { getTranslations, unstable_setRequestLocale } from 'next-intl/server';
import { getTranslations, setRequestLocale } from 'next-intl/server';
import ButtonLink from '~/components/Common/Button/Link/index.tsx';
import styles from './page.module.css';
import type { FC } from 'react';
import type { Metadata } from 'next';
import type { BaseParams } from '~/types/params.ts';

type NotFoundProps = {
params: BaseParams;
};
type NotFoundProps = BaseParams;

export const dynamic = 'force-static';

export const generateMetadata = async (): Promise<Metadata> => {
const t = await getTranslations('app.notFound');
Expand All @@ -20,7 +20,8 @@ export const generateMetadata = async (): Promise<Metadata> => {
};

const NotFound: FC<NotFoundProps> = async ({ params }) => {
unstable_setRequestLocale(params.locale);
const { locale } = await params;
setRequestLocale(locale);
const t = await getTranslations('app.notFound');

return (
Expand All @@ -35,6 +36,4 @@ const NotFound: FC<NotFoundProps> = async ({ params }) => {
);
};

export const dynamic = 'error';

export default NotFound;
10 changes: 5 additions & 5 deletions app/[locale]/about/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import type { Metadata } from 'next';
import type { AboutFrontmatter } from '~/types/frontmatter.ts';
import type { BaseParams } from '~/types/params.ts';

type PageProps = {
params: BaseParams;
};
type PageProps = BaseParams;

export const dynamic = 'force-static';

export const generateMetadata = async ({
params,
}: PageProps): Promise<Metadata | null> => {
const rawSource = await getContent({
section: 'about',
lang: params.locale,
lang: (await params).locale,
});

if (!rawSource) return null;
Expand All @@ -36,7 +36,7 @@ export const generateMetadata = async ({
const Page: FC<PageProps> = async ({ params }) => {
const rawSource = await getContent({
section: 'about',
lang: params.locale,
lang: (await params).locale,
});

if (!rawSource) notFound();
Expand Down
22 changes: 14 additions & 8 deletions app/[locale]/blog/[[...page]]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ import type { BaseParams } from '~/types/params.ts';
const MAX_POSTS_PER_PAGE = 6;

type PageProps = {
params: {
params: Promise<{
page?: string[];
} & BaseParams;
};
}>;
} & BaseParams;

type StaticParams = {
params: {
locale: string;
};
};

export const dynamic = 'force-static';

export const generateMetadata = async (): Promise<Metadata | null> => {
const t = await getTranslations('app.blog');

Expand All @@ -47,16 +49,20 @@ export const generateStaticParams = async ({ params }: StaticParams) => {
};

const Page: FC<PageProps> = async ({ params }) => {
const { page, locale } = await params;
let pageNumbers = null;
if (params.page && params.page[0] === 'index') {
if (page && page[0] === 'index') {
pageNumbers = 0;
} else if (params.page && Number.isInteger(parseInt(params.page[0], 10))) {
pageNumbers = parseInt(params.page[0], 10);
} else if (page && Number.isInteger(parseInt(page[0], 10))) {
pageNumbers = parseInt(page[0], 10);
}

if (pageNumbers === null) notFound();

const posts = await getSlugs({ section: 'blog', lang: params.locale });
const posts = await getSlugs({
section: 'blog',
lang: (await params).locale,
});
const metadata = await Promise.all(
posts
.slice(
Expand All @@ -66,7 +72,7 @@ const Page: FC<PageProps> = async ({ params }) => {
.map(slug =>
getFrontmatter<BlogFrontmatter>({
section: 'blog',
lang: params.locale,
lang: locale,
slug,
})
)
Expand Down
14 changes: 6 additions & 8 deletions app/[locale]/blog/post/[post]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import type { Metadata } from 'next';
import type { BlogFrontmatter } from '~/types/frontmatter.ts';
import type { BaseParams } from '~/types/params.ts';

type PageProps = {
params: {
post: string;
} & BaseParams;
type PageProps = BaseParams & {
params: Promise<{ post: string }>;
};

const slugToParams = (slug: string) => {
Expand All @@ -26,8 +24,8 @@ export const generateMetadata = async ({
}: PageProps): Promise<Metadata | null> => {
const rawSource = await getContent({
section: 'blog',
lang: params.locale,
slug: params.post,
lang: (await params).locale,
slug: (await params).post,
});

if (!rawSource) return null;
Expand Down Expand Up @@ -57,8 +55,8 @@ export const generateStaticParams = async () => {
const Page: FC<PageProps> = async ({ params }) => {
const rawSource = await getContent({
section: 'blog',
slug: params.post,
lang: params.locale,
slug: (await params).post,
lang: (await params).locale,
});

if (!rawSource) notFound();
Expand Down
12 changes: 5 additions & 7 deletions app/[locale]/feed/blog/rss.xml/route.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { generateRssFeed } from '~/lib/rss.ts';
import { availableLocales } from '~/utils/i18n/index.ts';
import { availableLocales } from '~/lib/i18n/config.ts';
import type { BaseParams } from '~/types/params.ts';

export const dynamic = 'force-static';

export const generateStaticParams = () => {
return availableLocales.map(lang => ({
locale: lang.code,
}));
};

type GETProps = {
params: BaseParams;
};

export const GET = async (_: Request, { params }: GETProps) => {
export const GET = async (_: Request, { params }: BaseParams) => {
const feed = await generateRssFeed({
section: 'blog',
lang: params.locale,
lang: (await params).locale,
});

return new Response(feed, {
Expand Down
30 changes: 15 additions & 15 deletions app/[locale]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import { getTranslations, unstable_setRequestLocale } from 'next-intl/server';
import { getTranslations, setRequestLocale } from 'next-intl/server';
import classNames from 'classnames';
import { GeistSans } from 'geist/font/sans';
import { GeistMono } from 'geist/font/mono';
import { LocaleProvider } from '~/providers/localeProvider.tsx';
import { availableLocales } from '~/utils/i18n/index.ts';
import { routing } from '~/lib/i18n/routing';
import type { FC, PropsWithChildren } from 'react';
import type { Metadata } from 'next';
import type { BaseParams } from '~/types/params.ts';
import '~/styles/globals.css';

type RootLayoutProps = PropsWithChildren<{
params: BaseParams;
}>;
type RootLayoutProps = PropsWithChildren<BaseParams>;

export const generateStaticParams = () => {
return availableLocales.map(lang => ({
locale: lang.code,
}));
};
export const dynamic = 'force-static';

// Generate params for all available locales
// It's allow us to build the website statically for all locales
export const generateStaticParams = () =>
routing.locales.map(locale => ({ locale }));

// @TODO: Generate metadata using i18n
export const generateMetadata = async (): Promise<Metadata | null> => {
const t = await getTranslations('app.metadata');

Expand All @@ -28,13 +26,15 @@ export const generateMetadata = async (): Promise<Metadata | null> => {
description: t('description'),
};
};
const RootLayout: FC<RootLayoutProps> = ({ children, params }) => {
unstable_setRequestLocale(params.locale);

const RootLayout: FC<RootLayoutProps> = async ({ children, params }) => {
const { locale } = await params;
setRequestLocale(locale);

return (
<html lang={params.locale}>
<html lang={locale}>
<body className={classNames(GeistSans.className, GeistMono.className)}>
<LocaleProvider locale={params.locale}>{children}</LocaleProvider>
<LocaleProvider locale={locale}>{children}</LocaleProvider>
</body>
</html>
);
Expand Down
10 changes: 5 additions & 5 deletions app/[locale]/projects/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import type { Metadata } from 'next';
import type { ProjectsFrontmatter } from '~/types/frontmatter.ts';
import type { BaseParams } from '~/types/params.ts';

type PageProps = {
params: BaseParams;
};
type PageProps = BaseParams;

export const dynamic = 'force-static';

export const generateMetadata = async ({
params,
}: PageProps): Promise<Metadata | null> => {
const rawSource = await getContent({
section: 'projects',
lang: params.locale,
lang: (await params).locale,
});

if (!rawSource) return null;
Expand All @@ -36,7 +36,7 @@ export const generateMetadata = async ({
const Page: FC<PageProps> = async ({ params }) => {
const rawSource = await getContent({
section: 'projects',
lang: params.locale,
lang: (await params).locale,
});

if (!rawSource) notFound();
Expand Down
6 changes: 3 additions & 3 deletions app/robots.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { MetadataRoute } from 'next';

function robots(): MetadataRoute.Robots {
export const dynamic = 'force-static';

export default function robots(): MetadataRoute.Robots {
return {
rules: {
userAgent: '*',
Expand All @@ -10,5 +12,3 @@ function robots(): MetadataRoute.Robots {
sitemap: 'https://augustinmauroy.github.io/sitemap.xml',
};
}

export default robots;
4 changes: 3 additions & 1 deletion app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { generateSitemap } from '~/lib/sitemap';
import { availableLocaleCodes } from '~/utils/i18n/index.ts';
import { availableLocaleCodes } from '~/lib/i18n/config.ts';
import type { MetadataRoute } from 'next';

export const dynamic = 'force-static';

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const sitemapEntries: MetadataRoute.Sitemap = [];

Expand Down
4 changes: 2 additions & 2 deletions components/Common/LanguageSelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { LanguageIcon } from '@heroicons/react/24/outline';
import { useTranslations } from 'next-intl';
import { useParams, usePathname, useRouter } from 'next/navigation';
import Dropdown from '~/components/Common/Dropdown';
import { availableLocales } from '~/utils/i18n';
import { availableLocales } from '~/lib/i18n/config.ts';

const LanguageSelector = () => {
const router = useRouter();
const pathname = usePathname();
const locale = useParams().locale;
const locale = useParams().locale ?? 'en';
const t = useTranslations('components.common.languageSelector');

const handleLocaleChange = (newLocale: string) => {
Expand Down
5 changes: 3 additions & 2 deletions utils/i18n/index.ts → lib/i18n/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import localeConfig from '~/i18n/config.json' with { type: 'json' };
import localeConfig from '~/i18n/config.json' assert { type: 'json' };

// As set of available and enabled locales for the website
// This is used for allowing us to redirect the user to any
Expand All @@ -10,7 +10,8 @@ const availableLocaleCodes = availableLocales.map(locale => locale.code);

// This provides the default locale information for the Next.js Application
// This is marked by the unique `locale.default` property on the `en` locale
const defaultLocale = availableLocales.find(locale => locale.default);
const defaultLocale =
availableLocales.find(locale => locale.default) || availableLocales[0];

// Creates a Map of available locales for easy access
const availableLocalesMap = Object.fromEntries(
Expand Down
25 changes: 17 additions & 8 deletions utils/i18n/config.ts → lib/i18n/request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getRequestConfig } from 'next-intl/server';
import { availableLocaleCodes } from './index.ts';
import { routing } from './routing.ts';
import { availableLocaleCodes } from './config.ts';

// Loads the Application Locales/Translations Dynamically
const loadLocaleDictionary = async (locale: string) => {
Expand All @@ -18,10 +19,18 @@ const loadLocaleDictionary = async (locale: string) => {
throw new Error(`Unsupported locale: ${locale}`);
};

// Provides `next-intl` configuration for RSC/SSR
export default getRequestConfig(async ({ locale }) => ({
// This is the dictionary of messages to be loaded
messages: await loadLocaleDictionary(locale),
// We always define the App timezone as UTC
timeZone: 'Etc/UTC',
}));
export default getRequestConfig(async ({ requestLocale }) => {
// This typically corresponds to the `[locale]` segment
let locale = await requestLocale;

// Ensure that the incoming locale is valid
if (!locale || !routing.locales.includes(locale))
locale = routing.defaultLocale;

const messages = await loadLocaleDictionary(locale);

return {
locale,
messages,
};
});
11 changes: 11 additions & 0 deletions lib/i18n/routing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { createNavigation } from 'next-intl/navigation';
import { defineRouting } from 'next-intl/routing';
import { availableLocaleCodes, defaultLocale } from './config.ts';

export const routing = defineRouting({
locales: availableLocaleCodes,
defaultLocale: defaultLocale.code,
});

export const { Link, redirect, usePathname, useRouter } =
createNavigation(routing);
2 changes: 1 addition & 1 deletion lib/sitemap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { availableLocaleCodes } from '~/utils/i18n/index.ts';
import { availableLocaleCodes } from './i18n/config.ts';
import { getSlugs, getFrontmatter } from './content.ts';
import type { MetadataRoute } from 'next';
import type { BlogFrontmatter } from '~/types/frontmatter.ts';
Expand Down
Loading

0 comments on commit 2d90748

Please sign in to comment.