Keeping pages static
Before Cache Components, route segment configs such as dynamic = 'force-static', dynamic = 'force-dynamic', and dynamic = 'error' selected a rendering mode for an entire route. Cache Components lets one route combine prerendered UI, cached content, and request-specific content rendered at request time.
This flexibility is useful for routes that need personalization. Use ensureStatic when some or all of a route must avoid request-time server rendering for predictable performance and compute costs.
Decide what should stay static
Static output does not depend on request-specific data. Next.js can generate it before a request and reuse it across requests.
With Partial Prefetching, route output becomes available in three navigation stages:
- A default
<Link>loads the App Shell. <Link prefetch={true}>can also load content for that link's URL.- The navigation renders anything that remains after the click.
Each ensureStatic level extends the static requirement through one of these stages. In the following diagram, gray marks static output and blue marks content that can render at request time:


Choose a level based on which request must use only static output:
'shell': the App Shell loaded by a default<Link>.'prefetch': the App Shell and the per-link prefetch from<Link prefetch={true}>, which can include content for the link's URL data, such asparams.'navigation': the complete route, including the response returned after the user navigates.
Example
The following example adds static requirements to three parts of an online store: its shared cart, product pages, and public buying guides.
Enable Cache Components and Partial Prefetching:
Step 1: Load a static App Shell
The shared store layout displays a visitor's cart count. CartButton reads the count from a cookie on the server:
The layout places CartButton inside <Suspense>, which provides a static fallback while the current count renders:
The navigation and cart fallback are shared across visitors. Loading a link to the store should not render the personalized cart count on the server before the user clicks. Add ensureStatic = 'shell' to make a default <Link> request only the static App Shell:
A default <Link> into the store now loads the navigation and Cart (0) fallback from static output. The server does not render the current cart count until a later per-link prefetch or navigation requests it. This avoids request-time server work for links that the user may never click.
Good to know
If only CartButton needed to be deferred from the App Shell, you could call await prefetch() inside that subtree. Here, the requirement applies to the entire store shell, including future changes, so the layout uses ensureStatic = 'shell'.
Step 2: Prefetch static product details
Each product page combines details that can be reused across visitors with a cart action that depends on the current visitor. ProductDetails caches its query with use cache, while ProductCartStatus reads a cookie to check whether the product is already in the cart:
The product route prerenders the catalog's product URL with generateStaticParams(). Separate <Suspense> boundaries let the product details render with a static cart fallback:
A default product link loads the App Shell, which contains the product fallback. Because the product details are already prerendered, the catalog can include them before the click by setting prefetch={true}:
Each visible prefetch={true} link can render ProductCartStatus to personalize the cart action for that product. A grid with 20 visible products can therefore trigger 20 request-time server renders before the user clicks a product.
Add ensureStatic = 'prefetch' to make every product prefetch use only static output:
Each product prefetch now includes the cached product details and the static Add to cart fallback without invoking request-time server rendering. The current cart status renders after the user follows the link.
An ensureStatic value covers the whole route, including layouts above the segment that exports it. On product routes, 'prefetch' also keeps the store layout static through the per-link prefetch.
Good to know
If only ProductCartStatus needed to be deferred from per-link prefetches, you could call await navigation() before reading the cookie. Here, every product prefetch must avoid request-time server rendering, so the page uses ensureStatic = 'prefetch'.
Step 3: Prerender the buying guides
The store also has public buying guides. Each guide reads its content from the database and applies a theme from a cookie:
The existing getGuide() helper performs the database query:
next build currently produces partial prerenders for the store routes and the buying guides:
Partial output is expected for the store and product routes because their request-specific cart UI renders later. The buying guides are public editorial pages, so they should return complete static output without request-time server rendering. Add ensureStatic = 'navigation':
When running next build again, Next.js first reports that the dynamic route does not provide any parameter values to prerender:
Export generateStaticParams() and return at least one buying guide ID. Next.js prerenders the returned IDs during the build. It can generate other allowed IDs on their first request and store the result for later requests. With 'navigation', that first request waits for the complete static page instead of streaming a fallback. See ISR with Cache Components.
Add a helper that returns the guide IDs to prerender:
generateStaticParams() provides the parameter values, but the guide still contains uncached and request-specific work. The next build reports that the route cannot be fully static:
The build identifies the affected route, but its stack frames do not include source locations for the data access. To get source-mapped code frames while fixing it, start next dev and open /guides/coffee. If you can only run a build, use next build --debug-prerender.
The development overlay first points to the uncached database query:
It presents three ways to handle uncached data:
The buying-guide content can be shared across visitors, so the Cache fix preserves the page's static requirement. Add the 'use cache' directive to getGuide() so Next.js can reuse the query result:
After the query is cached, the overlay points to the remaining request-specific value:
The overlay now presents the fixes for runtime data:
The buying guides do not need a visitor-specific theme, so remove the cookie access:
With the database query cached and the request-specific theme removed, next build produces a complete static page for the listed guide:
The listed guide is now fully prerendered. For an unlisted guide ID, the first request waits while Next.js generates and stores the complete static page. The buying guides no longer perform request-time server rendering, and ensureStatic will report future changes that would break that requirement.
The store now loads shared navigation from its static App Shell, includes cached product details in per-link prefetches without invoking the server, and returns complete static output for buying guides. Personalized cart data still renders when it is needed.
Next steps
- Optimize prefetching to control when individual subtrees render.
- Ensure instant navigations to validate that client navigations into the store show UI immediately.
- Migrate to Cache Components to replace route-level configs such as
dynamic = 'force-static'in an existing app.
API reference
Require static output across a route or defer work within one subtree.
ensureStatic
prefetch
navigation
Last updated