#viewport, metadata and not nested
1 messages · Page 1 of 1 (latest)
Hi @gaunt tendon. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In Next.js, you can define the viewport and metadata for your entire application in the root layout file and then override them on a per-page basis if needed. However, there are some important points to note:
- Viewport: You can customize the initial viewport of the page with the static
viewportobject or the dynamicgenerateViewportfunction. These are only supported in Server Components and you cannot export both from the same route segment. If the viewport doesn't depend on runtime information, it should be defined using the staticviewportobject rather thangenerateMetadata. Here's an example of how to define the viewport options:
import type { Viewport } from 'next'
export const viewport: Viewport = {
themeColor: 'black',
}
export default function Page() {}
-
Metadata: The
viewportoption inmetadatais deprecated as of Next.js 14. You should use theviewportconfiguration instead. Metadata is evaluated in order, starting from the root segment down to the segment closest to the finalpage.jssegment. Metadata objects exported from multiple segments in the same route are shallowly merged together to form the final metadata output of a route. Duplicate keys are replaced based on their ordering. This means metadata with nested fields that are defined in an earlier segment are overwritten by the last segment to define them. -
Overriding: File-based metadata has the higher priority and will override any config-based metadata. You can overwrite the default
viewportmeta tag.
If you're migrating metadata exports, you can use metadata-to-viewport-export codemod (https://nextjs.org/docs/app/building-your-application/upgrading/codemods#metadata-to-viewport-export) to update your changes.
For more details, you can refer to the following documentation:
- generateViewport (https://nextjs.org/docs/app/api-reference/functions/generate-viewport)
- Metadata Object and generateMetadata Options (https://nextjs.org/docs/app/api-reference/functions/generate-metadata)
- Optimizing Metadata (https://nextjs.org/docs/app/building-your-application/optimizing/metadata)
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@gaunt tendon kapa.ai is still learning and improving, please let us know how it did by reacting below
@dry jetty viewport is simply not working, just not making any changes for any page
Let me check the knowledge base for you. One moment! :books:
If the viewport settings are not reflecting on your pages, there could be a few reasons for this:
-
Server Components: The
viewportobject andgenerateViewportfunction exports are only supported in Server Components. Make sure you're using Server Components. -
Exporting Both: You cannot export both the
viewportobject andgenerateViewportfunction from the same route segment. If you're doing this, it could be causing the issue. -
Version: There was a known issue in Next.js where the viewport meta tag was missing in the HTML, and the viewport value was incorrect. This issue was reported in version 14.0.0 and was fixed in version 14.0.1. Make sure you're using the latest version of Next.js. If you're using an older version, consider updating it.
-
Migration: If you're migrating from
metadataexports, make sure you've used the metadata-to-viewport-export codemod (https://nextjs.org/docs/app/building-your-application/upgrading/codemods#metadata-to-viewport-export) to update your changes.