Skip to content

Use React.cache() to deduplicate getPage between Page and generateMetadata in docs #1055

@MODSetter

Description

@MODSetter

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

  1. Import cache from React and create a shared cached function:
import { cache } from "react";

const getDocPage = cache((slug?: string[]) => {
  return source.getPage(slug);
});
  1. 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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions