generateViewport
API Reference for the generateViewport function.
You can customize the initial viewport of the page with the static viewport object or the dynamic generateViewport function.
Good to know:
- The
viewportobject andgenerateViewportfunction exports are only supported in Server Components. - You cannot export both the
viewportobject andgenerateViewportfunction from the same route segment. - If you're coming from migrating
metadataexports, you can use metadata-to-viewport-export codemod to update your changes.
The viewport object
To define the viewport options, export a viewport object from a layout.jsx or page.jsx file.
generateViewport function
generateViewport should return a Viewport object containing one or more viewport fields.
In TypeScript, the params argument can be typed via PageProps<'/route'> or LayoutProps<'/route'> depending on where generateViewport is defined.
Good to know:
- If the viewport doesn't depend on runtime information, it should be defined using the static
viewportobject rather thangenerateViewport.
Viewport Fields
themeColor
Learn more about theme-color.
Simple theme color
With media attribute
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.
colorScheme
Learn more about color-scheme.
Types
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.
viewport object
generateViewport function
Regular function
With segment props
JavaScript Projects
For JavaScript projects, you can use JSDoc to add type safety.
Version History
| Version | Changes |
|---|---|
v14.0.0 | viewport and generateViewport introduced. |