Skip to content

generateViewport

Source URL: https://nextjs.org/docs/app/api-reference/functions/generate-viewport

You can customize the initial viewport of the page with the static viewport object or the dynamic generateViewport function.

Good to know:

  • The viewport object and generateViewport function exports are only supported in Server Components.
  • You cannot export both the viewport object and generateViewport function from the same route segment.
  • If you’re coming from migrating metadata exports, you can use metadata-to-viewport-export codemod to update your changes.

To define the viewport options, export a viewport object from a layout.jsx or page.jsx file.

import type { Viewport } from 'next'
export const viewport: Viewport = {
themeColor: 'black',
}
export default function Page() {}
export const viewport = {
themeColor: 'black',
}
export default function Page() {}

generateViewport should return a Viewport object containing one or more viewport fields.

export function generateViewport({ params }) {
return {
themeColor: '...',
}
}

In TypeScript, the params argument can be typed via PageProps<'/route'> or LayoutProps<'/route'> depending on where generateViewport is defined.

export function generateViewport({ params }) {
return {
themeColor: '...',
}
}

Good to know:

  • If the viewport doesn’t depend on runtime information, it should be defined using the static viewport object rather than generateViewport.

Learn more about theme-color.

Simple theme color

import type { Viewport } from 'next'
export const viewport: Viewport = {
themeColor: 'black',
}
export const viewport = {
themeColor: 'black',
}
<meta name="theme-color" content="black" />

With media attribute

import type { Viewport } from 'next'
export const viewport: Viewport = {
themeColor: [
{ media: '(prefers-color-scheme: light)', color: 'cyan' },
{ media: '(prefers-color-scheme: dark)', color: 'black' },
],
}
export const viewport = {
themeColor: [
{ media: '(prefers-color-scheme: light)', color: 'cyan' },
{ media: '(prefers-color-scheme: dark)', color: 'black' },
],
}
<meta name="theme-color" media="(prefers-color-scheme: light)" content="cyan" />
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="black" />

width, initialScale, maximumScale and userScalable

Section titled “width, initialScale, maximumScale and userScalable”

Good to know: The viewport meta tag is automatically set, and manual configuration is usually unnecessary as the default is sufficient. However, the information is provided for completeness.

import type { Viewport } from 'next'
export const viewport: Viewport = {
width: 'device-width',
initialScale: 1,
maximumScale: 1,
userScalable: false,
// Also supported but less commonly used
// interactiveWidget: 'resizes-visual',
}
export const viewport = {
width: 'device-width',
initialScale: 1,
maximumScale: 1,
userScalable: false,
// Also supported but less commonly used
// interactiveWidget: 'resizes-visual',
}
<meta
name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"
/>

Learn more about color-scheme.

import type { Viewport } from 'next'
export const viewport: Viewport = {
colorScheme: 'dark',
}
export const viewport = {
colorScheme: 'dark',
}
<meta name="color-scheme" content="dark" />

When Cache Components is enabled, generateViewport follows the same rules as other components. If viewport accesses runtime data (cookies(), headers(), params, searchParams) or performs uncached data fetching, it defers to request time.

Unlike metadata, viewport cannot be streamed because it affects initial page load UI. If generateViewport defers to request time, the page would need to block until resolved.

If viewport depends on external data but not runtime data, use use cache:

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

If viewport genuinely requires runtime data, wrap the document <body> in a Suspense boundary to signal that the entire route should be dynamic:

import { Suspense } from 'react'
import { cookies } from 'next/headers'
export async function generateViewport() {
const cookieJar = await cookies()
return {
themeColor: cookieJar.get('theme-color')?.value,
}
}
export default function RootLayout({ children }) {
return (
<Suspense>
<html>
<body>{children}</body>
</html>
</Suspense>
)
}

Caching is preferred because it allows static shell generation. Wrapping the document body in Suspense means there is no static shell or content to immediately send when a request arrives, making the entire route block until ready on every request.

Good to know: Use multiple root layouts to isolate fully dynamic viewport to specific routes, while still letting other routes in your application generate a static shell.

You can add type safety to your viewport object by using the Viewport type. If you are using the built-in TypeScript plugin in your IDE, you do not need to manually add the type, but you can still explicitly add it if you want.

import type { Viewport } from 'next'
export const viewport: Viewport = {
themeColor: 'black',
}
import type { Viewport } from 'next'
export function generateViewport(): Viewport {
return {
themeColor: 'black',
}
}
import type { Viewport } from 'next'
type Props = {
params: Promise<{ id: string }>
searchParams: Promise<{ [key: string]: string | string[] | undefined }>
}
export function generateViewport({ params, searchParams }: Props): Viewport {
return {
themeColor: 'black',
}
}
export default function Page({ params, searchParams }: Props) {}

For JavaScript projects, you can use JSDoc to add type safety.

/** @type {import("next").Viewport} */
export const viewport = {
themeColor: 'black',
}
VersionChanges
v14.0.0viewport and generateViewport introduced.

View all the Metadata API options.

  • Metadata Files
    • API documentation for the metadata file conventions.
  • Cache Components
    • Learn how to use Cache Components and combine the benefits of static and dynamic rendering.
  • cacheComponents
    • Learn how to enable the cacheComponents flag in Next.js.