Skip to content

Commit 7fcdbf1

Browse files
authored
Using Cache Components (#218)
* Using Cache Components - Updated various dependencies in `package.json` and `bun.lock`, including `@graphql-codegen` packages and `ai`. - Added caching configuration in `next.config.ts` and implemented caching strategies in multiple components to improve performance. - Refactored locale handling in `locale-switcher.tsx` and updated routing logic in `sitemap.ts` and layout files. - Removed unused service worker files to streamline the project. * Refactor static parameter generation in layout and page components - Updated the `generateStaticParams` function in `layout.tsx` and `page.tsx` to use `flatMap` instead of `map` for improved handling of locale and sheet combinations.
1 parent 6bdfcf3 commit 7fcdbf1

File tree

12 files changed

+64
-1474
lines changed

12 files changed

+64
-1474
lines changed

app/[locale]/[sheet]/layout.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
"use cache";
12
import type { Metadata } from "next";
3+
import { cacheLife } from "next/cache";
24
import DocumentSheet from "@/document-sheet";
35
import { i18n } from "../../i18n-config";
46

5-
export const runtime = "edge";
6-
export const revalidate = 60; // 1 minute
7-
87
type Props = LayoutProps<"/[locale]/[sheet]">;
98

10-
export default function SheetLayout({ children }: Props) {
9+
export default async function SheetLayout({ children }: Props) {
10+
cacheLife("hours");
1111
return <DocumentSheet>{children}</DocumentSheet>;
1212
}
1313

@@ -47,3 +47,10 @@ const createTitle = (sheet: string) =>
4747
.split("-")
4848
.map((word) => word[0].toUpperCase() + word.slice(1))
4949
.join(" ");
50+
51+
export async function generateStaticParams() {
52+
const sheets = ["skill-profile", "cv", "legal", "privacy"] as const;
53+
return i18n.locales.flatMap((locale) =>
54+
sheets.map((sheet) => ({ locale, sheet }))
55+
);
56+
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
"use cache";
12
import { documentToReactComponents } from "@contentful/rich-text-react-renderer";
23
import { INLINES } from "@contentful/rich-text-types";
34
import { ArrowUpRightIcon } from "@heroicons/react/20/solid";
45
import type { AllInOnePageQuery } from "gql/graphql";
6+
import { cacheLife } from "next/cache";
57
import type { Article, WithContext } from "schema-dts";
68
import { DocumentSheetContent } from "@/document-sheet";
79
import { graphqlClient } from "@/graphql-client";
10+
import { i18n } from "@/i18n-config";
811
import { pageQuery } from "../page-query";
912

10-
export const runtime = "edge";
11-
export const revalidate = 60; // 1 minute
12-
1313
export default async function Page({ params }: PageProps<"/[locale]/[sheet]">) {
14+
cacheLife("hours");
1415
const { sheet, locale } = await params;
1516

1617
const { allInOnePageCollection } =
@@ -77,3 +78,10 @@ const jsonLd: WithContext<Article> = {
7778
articleBody:
7879
"Manuel Dugué is a media computer scientist born in Berlin with German and French roots. He is passionate about sports, music, photography, typography, architecture, and cooking. Manuel is a language enthusiast, fluent in German, French, English, and Spanish, with basic knowledge of Portuguese and Dutch. He has programming skills in TypeScript, JavaScript, HTML, and more, and loves working with frameworks like React, D3.js, and Next.js. Manuel has had an exciting career working on projects like digital exhibitions, family trees, and an online shop for garden culture. He has also done some cool stuff with Android tablets for seniors and interactive presentation films. Manuel has a diploma in Media Informatics from TU Dresden and a research thesis on 'materiality and interaction.' He has won awards like the arctic code vault contributor and a couple of photo competitions. Manuel is a tech-savvy, creative, and multilingual individual.",
7980
};
81+
82+
export async function generateStaticParams() {
83+
const sheets = ["skill-profile", "cv", "legal", "privacy"] as const;
84+
return i18n.locales.flatMap((locale) =>
85+
sheets.map((sheet) => ({ locale, sheet }))
86+
);
87+
}

app/[locale]/layout.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use cache";
12
import {
23
RiCupFill,
34
RiGithubFill,
@@ -6,6 +7,7 @@ import {
67
RiTwitterXFill,
78
} from "@remixicon/react";
89
import type { Metadata } from "next";
10+
import { cacheLife } from "next/cache";
911
import {
1012
Bungee,
1113
Bungee_Inline,
@@ -24,9 +26,6 @@ import LocaleSwitcher from "@/locale-switcher";
2426
import "./globals.css";
2527
import { ErrorBoundary } from "react-error-boundary";
2628

27-
export const runtime = "edge";
28-
export const revalidate = 60; // 1 minute
29-
3029
const montserrat = Montserrat({
3130
subsets: ["latin"],
3231
variable: "--font-montserrat",
@@ -52,6 +51,7 @@ export default async function MyApp({
5251
children,
5352
params,
5453
}: LayoutProps<"/[locale]">) {
54+
cacheLife("hours");
5555
const { locale } = await params;
5656
assertLocale(locale);
5757
return (
@@ -313,3 +313,7 @@ export async function generateMetadata({
313313
],
314314
};
315315
}
316+
317+
export async function generateStaticParams() {
318+
return i18n.locales.map((locale) => ({ locale }));
319+
}

app/[locale]/page.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
export const runtime = "edge";
2-
export const revalidate = 60; // 1 minute
3-
41
export default function Page() {
52
return;
63
}

app/landing-page-quote.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
"use cache";
12
import { documentToHtmlString } from "@contentful/rich-text-html-renderer";
23
import { RiOpenaiFill } from "@remixicon/react";
34
import { kv } from "@vercel/kv";
45
import type { AllInOnePageQuery } from "gql/graphql";
6+
import { cacheLife } from "next/cache";
57
import Link from "next/link";
68
import OpenAi from "openai";
79
import { Suspense } from "react";
@@ -12,17 +14,10 @@ import type { Locale } from "@/i18n-config";
1214

1315
const openai = new OpenAi({
1416
apiKey: process.env.OPENAI_API_KEY,
15-
fetch: (input, init) =>
16-
fetch(input, {
17-
...init,
18-
next: {
19-
/*1 day: 60 * 60 * 24*/
20-
revalidate: 86_400,
21-
},
22-
}),
2317
});
2418

2519
export default async function Quote(props: { locale: Locale }) {
20+
cacheLife("days");
2621
const { locale } = props;
2722
const { allInOnePageCollection } =
2823
await graphqlClient.request<AllInOnePageQuery>(pageQuery, {

app/locale-switcher.tsx

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
1-
"use client";
2-
31
import Link from "next/link";
4-
import { usePathname } from "next/navigation";
52
import { i18n, type Locale } from "../app/i18n-config";
63

74
export default function LocaleSwitcher(props: {
85
className?: string;
96
currentLocale: Locale;
107
}) {
118
const { currentLocale } = props;
12-
const pathName = usePathname();
13-
const redirectedPathName = (locale: string) => {
14-
if (!pathName) {
15-
return "/";
16-
}
17-
const segments = pathName.split("/");
18-
segments[1] = locale;
19-
return segments.join("/");
20-
};
219

2210
return (
2311
<div>
@@ -30,7 +18,7 @@ export default function LocaleSwitcher(props: {
3018
? "text-teal-500"
3119
: "hover:text-gray-400"
3220
}`}
33-
href={redirectedPathName(locale)}
21+
href={`/${locale}`}
3422
>
3523
{locale}
3624
</Link>

app/sitemap.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
"use cache";
12
import type { MetadataRoute } from "next";
3+
import { cacheLife } from "next/cache";
24
import { i18n } from "./i18n-config";
35

4-
export const runtime = "edge";
5-
export const revalidate = 86_400; // 60 * 60 * 24 1 day
6-
7-
export default function sitemap(): MetadataRoute.Sitemap {
6+
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
7+
cacheLife("days");
88
return i18n.locales.flatMap((locale) => [
99
{ url: `https://manuel.fyi/${locale}`, lastModified: new Date() },
1010
{

0 commit comments

Comments
 (0)