Dynamic route viewport settings
A route's theme color or color scheme depends on route-specific data.
Next.js 16+ · Function
generateViewport dynamically defines a route's initial viewport configuration in a Next.js App Router page or layout. Return a Viewport object; use the static viewport export for settings that do not need dynamic computation.
Export generateViewport from a layout or page when viewport settings must be computed dynamically. It returns a Viewport object; use the static viewport export instead when the values do not depend on dynamic data.
export function generateViewport({ params }): Viewport { return { ... } }| Name | Type | Description |
|---|---|---|
params | object | The route parameters supplied to the page or layout; available when the viewport depends on dynamic route data. |
A Viewport object containing one or more viewport fields.
import type { Viewport } from 'next'
export function generateViewport(): Viewport {
return {
themeColor: 'black',
colorScheme: 'dark',
width: 'device-width',
initialScale: 1,
}
}
export default function Layout({
children,
}: Readonly<{ children: React.ReactNode }>) {
return <html lang="en"><body>{children}</body></html>
}This server-component layout computes the initial viewport configuration and returns it as a typed Viewport object.
A route's theme color or color scheme depends on route-specific data.
A layout needs to compute viewport fields from external data.
The application needs a typed viewport declaration in TypeScript.
viewportViewportPagePropsLayoutPropsthemeColorcolorScheme