Overview
The framework employs a robust caching strategy to ensure fast load times and reduce server load. It automatically minimizes network requests and optimizes content delivery by leveraging Next.js caching mechanisms. The caching system operates on three levels:- Request Memoization: Deduplicates requests within the same render pass.
- Data Cache: Caches the results of fetch requests across requests.
- Full Route Cache: Stores the rendered HTML and React Server Component payload at build time.
Request Memoization
The framework uses thecache function from React to memoize data fetching functions. This internal mechanism ensures that if the same data is needed in multiple components or during different phases of rendering (e.g., getting metadata and rendering the page), the framework triggers only one network request.
A clear example of this usage is found in app/page.tsx. This file calls getHomePage() in two distinct places:
- Inside
generateMetadatato fetch the page title and SEO attributes. - Inside the
Indexcomponent to render the actual page content.
app/page.tsx
cache function prevents this redundancy, ensuring the Index component uses the data already fetched by generateMetadata.
Data Cache & Revalidation
The framework caches data fetched from the CMS to maintain high performance. It manages the freshness of this data through a specific configuration found in files likeapp/page.tsx and app/layout.tsx.
The system uses a headers object in its internal fetch calls that references an environment variable:
app/page.tsx
- Configuration: The
REQUEST_REVALIDATEenvironment variable defines the cache lifetime (in seconds) for these fetches. - Behavior: The framework passes this configuration to every request it makes to the CMS.
Full Route Cache (ISR)
The framework utilizes Incremental Static Regeneration (ISR) to cache rendered pages, reducing the load on the server. This is primarily configured in dynamic page routes likeapp/(pages)/[...slug]/page.tsx.
Static Generation
By default, the framework statically generates pages. It sets a revalidation period to keep the rendered route cached effectively indefinitely until a new build or revalidation event occurs.app/(pages)/[...slug]/page.tsx
Dynamic Routes
For dynamic routes, the framework internally usesgenerateStaticParams to identify which paths to pre-render during the build process.
app/(pages)/[...slug]/page.tsx
- Pre-rendering: The build process generates static files for all known pages.
- On-Demand Generation: The framework is configured (
dynamicParams = true) to generate new pages on the first request if they were not created at build time, ensuring that new content is always accessible.