Skip to content

Commit 98417e2

Browse files
committed
perf: move 'use cache' to the data layer with cacheLife('max')
Per review feedback from @icyJoseph: instead of 'use cache' on every page and generateMetadata, cache at the utility/data layer so callers just work and the page render + generateMetadata share one compile. - readMarkdownPage, collectSectionPaths, collectFlatSectionSlugs, loadErrorCodes, and a new compileErrorDecoderData helper are now 'use cache' + cacheLife('max') (stable content, only changes on deploy; revalidate 30d instead of the default 15m). - loadErrorDecoderData stays uncached and keeps the notFound() check, which can't run inside a 'use cache' scope; it delegates the cached compile to compileErrorDecoderData. - Removed 'use cache' from all 9 routes and their generateMetadata. Build unchanged: 816 routes, all Static / Partial Prerender.
1 parent b459abc commit 98417e2

12 files changed

Lines changed: 35 additions & 24 deletions

File tree

src/app/blog/[[...slug]]/page.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export async function generateStaticParams() {
2121
}
2222

2323
export async function generateMetadata({params}: PageProps): Promise<Metadata> {
24-
'use cache';
2524
const {slug} = await params;
2625
return sectionPageMetadata({
2726
section: 'blog',
@@ -31,7 +30,6 @@ export async function generateMetadata({params}: PageProps): Promise<Metadata> {
3130
}
3231

3332
export default async function BlogPage({params}: PageProps) {
34-
'use cache';
3533
const {slug} = await params;
3634
return renderSectionPage({
3735
section: 'blog',

src/app/community/[[...slug]]/page.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export async function generateStaticParams() {
2121
}
2222

2323
export async function generateMetadata({params}: PageProps): Promise<Metadata> {
24-
'use cache';
2524
const {slug} = await params;
2625
return sectionPageMetadata({
2726
section: 'community',
@@ -30,7 +29,6 @@ export async function generateMetadata({params}: PageProps): Promise<Metadata> {
3029
}
3130

3231
export default async function CommunityPage({params}: PageProps) {
33-
'use cache';
3432
const {slug} = await params;
3533
return renderSectionPage({
3634
section: 'community',

src/app/errors/[errorCode]/page.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ export async function generateStaticParams() {
1919
}
2020

2121
export async function generateMetadata({params}: PageProps): Promise<Metadata> {
22-
'use cache';
2322
const {errorCode} = await params;
2423
return {title: `Minified React error #${errorCode}`};
2524
}
2625

2726
export default async function ErrorDecoderPage({params}: PageProps) {
28-
'use cache';
2927
const {errorCode} = await params;
3028
const data = await loadErrorDecoderData(errorCode);
3129
return <ErrorDecoderView data={data} pathname={`/errors/${errorCode}`} />;

src/app/errors/page.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export const metadata: Metadata = {
1414
};
1515

1616
export default async function ErrorDecoderIndex() {
17-
'use cache';
1817
const data = await loadErrorDecoderData(null);
1918
return <ErrorDecoderView data={data} pathname="/errors" />;
2019
}

src/app/learn/[[...slug]]/page.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export async function generateStaticParams() {
2121
}
2222

2323
export async function generateMetadata({params}: PageProps): Promise<Metadata> {
24-
'use cache';
2524
const {slug} = await params;
2625
return sectionPageMetadata({
2726
section: 'learn',
@@ -31,7 +30,6 @@ export async function generateMetadata({params}: PageProps): Promise<Metadata> {
3130
}
3231

3332
export default async function LearnPage({params}: PageProps) {
34-
'use cache';
3533
const {slug} = await params;
3634
return renderSectionPage({
3735
section: 'learn',

src/app/page.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ import {buildPageMetadata} from 'lib/buildPageMetadata';
1313
import {DocsPage} from './DocsPage';
1414

1515
export async function generateMetadata(): Promise<Metadata> {
16-
'use cache';
1716
const data = await readMarkdownPage([]);
1817
return buildPageMetadata({data, pathname: '/', section: 'home'});
1918
}
2019

2120
export default async function HomePage() {
22-
'use cache';
2321
const data = await readMarkdownPage([]);
2422
return (
2523
<DocsPage

src/app/reference/[[...slug]]/page.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export async function generateStaticParams() {
2121
}
2222

2323
export async function generateMetadata({params}: PageProps): Promise<Metadata> {
24-
'use cache';
2524
const {slug} = await params;
2625
return sectionPageMetadata({
2726
section: 'reference',
@@ -30,7 +29,6 @@ export async function generateMetadata({params}: PageProps): Promise<Metadata> {
3029
}
3130

3231
export default async function ReferencePage({params}: PageProps) {
33-
'use cache';
3432
const {slug} = await params;
3533
return renderSectionPage({
3634
section: 'reference',

src/app/versions/page.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ import type {RouteItem} from 'components/Layout/getRouteMeta';
1111
import {renderSectionPage, sectionPageMetadata} from '../renderSectionPage';
1212

1313
export async function generateMetadata(): Promise<Metadata> {
14-
'use cache';
1514
return sectionPageMetadata({section: 'unknown', segments: ['versions']});
1615
}
1716

1817
export default async function VersionsPage() {
19-
'use cache';
2018
return renderSectionPage({
2119
section: 'unknown',
2220
segments: ['versions'],

src/app/warnings/[slug]/page.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export async function generateStaticParams() {
2121
}
2222

2323
export async function generateMetadata({params}: PageProps): Promise<Metadata> {
24-
'use cache';
2524
const {slug} = await params;
2625
return sectionPageMetadata({
2726
section: 'unknown',
@@ -30,7 +29,6 @@ export async function generateMetadata({params}: PageProps): Promise<Metadata> {
3029
}
3130

3231
export default async function WarningPage({params}: PageProps) {
33-
'use cache';
3432
const {slug} = await params;
3533
return renderSectionPage({
3634
section: 'unknown',

src/lib/collectPaths.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'server-only';
99
import fs from 'fs';
1010
import path from 'path';
1111
import {promisify} from 'util';
12+
import {cacheLife} from 'next/cache';
1213

1314
const readdir = promisify(fs.readdir);
1415
const stat = promisify(fs.stat);
@@ -46,6 +47,8 @@ function getSegments(file: string): string[] {
4647
export async function collectSectionPaths(
4748
section: string
4849
): Promise<string[][]> {
50+
'use cache';
51+
cacheLife('max');
4952
const dir = path.join(ROOT, section);
5053
if (!fs.existsSync(dir)) return [];
5154
const files = await getFiles(dir, dir);
@@ -64,6 +67,8 @@ export async function collectSectionPaths(
6467
export async function collectFlatSectionSlugs(
6568
section: string
6669
): Promise<string[]> {
70+
'use cache';
71+
cacheLife('max');
6772
const dir = path.join(ROOT, section);
6873
if (!fs.existsSync(dir)) return [];
6974
const entries = await readdir(dir);

0 commit comments

Comments
 (0)