Description
In app/docs/[[...slug]]/page.tsx, both the Page component and generateMetadata independently call source.getPage(params.slug) for the same request, duplicating CPU work.
Vercel React Best Practices Rule: server-cache-react (3.7)
File to change
surfsense_web/app/docs/[[...slug]]/page.tsx
What to do
- Import
cache from React and create a shared cached function:
import { cache } from "react";
const getDocPage = cache((slug?: string[]) => {
return source.getPage(slug);
});
- Use it in both
Page and generateMetadata:
export default async function Page(props: { params: Promise<{ slug?: string[] }> }) {
const params = await props.params;
const page = getDocPage(params.slug);
if (!page) notFound();
// ...
}
export async function generateMetadata(props: { params: Promise<{ slug?: string[] }> }) {
const params = await props.params;
const page = getDocPage(params.slug);
if (!page) notFound();
return { title: page.data.title, description: page.data.description };
}
React.cache() deduplicates within a single request, so the second call is a cache hit.
Acceptance criteria
- Only one
getPage call executes per request (both Page and generateMetadata share the result)
- Docs pages still render with correct content and metadata
- No regressions in docs navigation
Description
In
app/docs/[[...slug]]/page.tsx, both thePagecomponent andgenerateMetadataindependently callsource.getPage(params.slug)for the same request, duplicating CPU work.Vercel React Best Practices Rule:
server-cache-react(3.7)File to change
surfsense_web/app/docs/[[...slug]]/page.tsxWhat to do
cachefrom React and create a shared cached function:PageandgenerateMetadata:React.cache()deduplicates within a single request, so the second call is a cache hit.Acceptance criteria
getPagecall executes per request (bothPageandgenerateMetadatashare the result)