Back to Next Js

Next.js encountered uncached data in generateViewport()

errors/blocking-prerender-viewport-dynamic.mdx

16.3.011.4 KB
Original Source
<div style={{ padding: '1.25rem 1.5rem', border: '1px solid var(--ds-gray-400)', borderRadius: '12px', background: 'var(--ds-background-200)', margin: '1.5rem 0 2rem', fontSize: '0.95rem', lineHeight: '1.6', }} > This Insight is part of the [Instant Navigations](https://nextjs.org/blog/next-16-3-instant-navigations) feature introduced in Next.js 16.3. If you're new to it, start with the [Ensuring instant navigations](https://preview.nextjs.org/docs/app/guides/instant-navigation) guide for an overview of what instant navigations are and how Next.js validates them, then come back here for the specific fix. </div>

During prerendering, generateViewport() performed an uncached data access (fetch(), database call, await connection()). With Cache Components enabled, viewport metadata can't be deferred behind a <Suspense> boundary because it affects the initial page load. The page can't be prerendered, so navigations block instead of being instant.

Request-bound reads (cookies(), headers(), params, searchParams) in generateViewport() have different fixes. See Next.js encountered runtime data in generateViewport(). The metadata equivalent is handled at Uncached data in generateMetadata(). For errors in the page body rather than viewport, see Next.js encountered uncached data during prerendering.

Ways to fix this

<FixCardGrid> <FixCard group="cache" title="Cache the viewport data" href="#cache-the-viewport-data" snippets={[ { text: 'async function generateViewport() {' }, { text: ' "use cache"', highlight: true }, { text: ' return await db.getViewport(…)' }, ]} /> <FixCard group="block" title="Allow blocking route" href="#allow-blocking-route" snippets={[ { text: '// page.tsx or layout.tsx' }, { text: 'export const instant = false', highlight: true }, ]} /> </FixCardGrid>

Cache the viewport data

Choose this fix when the viewport values come from an external source (database, CMS) but don't need to change on every request. Add the use cache directive as the first statement inside generateViewport(). Next.js caches the returned viewport object and includes it in the prerender.

This fix does not apply to connection(). The point of connection() is to opt into per-request rendering, so caching it would defeat the purpose. Use Allow blocking route instead.

Patterns

Add use cache to generateViewport

Mark the function as cacheable. The viewport is evaluated once per cache window and reused.

jsx
import { db } from './db'

export async function generateViewport() {
  'use cache'
  const { width, initialScale } = await db.query('viewport-config')
  return { width, initialScale }
}

export default function RootLayout({ children }) {
  return (
    <html>
      <body>{children}</body>
    </html>
  )
}

Learn more: Caching with use cache.

Trade-off

Freshness depends on the cache configuration. The viewport stays the same until cacheLife expires or cacheTag is invalidated.

Gotchas

  • Inside a use cache scope, you can't call cookies() or headers(). If the viewport needs a request-bound value (a theme color from a cookie), use Allow blocking route instead.
  • A short cacheLife (a profile whose revalidate is shorter than the prerender's effective lifetime) prevents the viewport from being included in the prerender. Use a longer profile if you want the viewport included in the static shell.

Allow blocking route

Choose this fix when the viewport data is genuinely uncacheable. Setting instant to false exempts the segment from instant-navigation validation. The page renders on every request and the navigation blocks until that render completes.

Unlike page body content, viewport metadata can't be deferred behind <Suspense> because it affects the initial HTML <head>. Making the viewport dynamic means the entire page navigation blocks.

Patterns

Opt the layout out

Set instant to false on the layout that defines generateViewport. This allows that layout segment to block while descendant segments remain independently validated. Apply this to the nested layout that owns the dynamic viewport, not the root layout, so the opt-out is scoped to the affected segment.

jsx
import { db } from './db'

export const instant = false

export async function generateViewport() {
  const { width, initialScale } = await db.query('viewport-config')
  return { width, initialScale }
}

export default function DashboardLayout({ children }) {
  return children
}

Learn more: Route segment instant config.

Use this pattern when:

  • The viewport data is uncacheable and must be fetched on every request.
  • You're migrating a route incrementally and want to defer the lifetime decision without changing how the page renders today.

Don't use this to dismiss the error. Choose Cache the viewport data when feasible.

Trade-off

Navigations to this route are not instant. The user waits for the full server render before any HTML arrives. Use this only when that latency is necessary for the route to function.

Gotchas

  • Setting instant to false opts only the segment that exports it out. Descendant segments are still validated by the global default.
  • This export does not disable prerendering. The route still prerenders if it can. It only disables instant-navigation validation for the route.
  • Framework-synthesized routes (/_not-found, /_global-error) inherit the root layout's generateViewport and must be statically prerendered. instant = false opts the route out of validation but does not let those routes through, so the build still fails when they prerender. If your root layout's generateViewport depends on request data, Cache the viewport data instead, or move to global-not-found.js, which bypasses the root layout entirely and avoids inheriting its generateViewport.

Verifying the fix

After applying a fix, reload the route and confirm the page immediately paints meaningful UI, with any <Suspense> fallbacks covering only the regions that stream in. A <Suspense> boundary placed around the whole page body can pass validation with an empty shell, which defeats the point of an instant navigation.

In next dev, the error overlay points at the failing component with file paths and line numbers. When working from a build instead, the default next build output is more abbreviated. Run next build --debug-prerender for full user-frame stack traces and next build --debug-build-paths /dashboard /settings to iterate on specific routes.

Don't want this validation?

Instant-navigation validation runs by default in Cache Components apps and is what surfaces this error.

See Ensuring instant navigations for the full model.