Nextjs Metadata Template Explained: A Step-by-Step Guide

Author

Reads 1.3K

Webpage of Ai Chatbot, a prototype AI Smith Open chatbot, is seen on the website of OpenAI, on a apple smartphone. Examples, capabilities, and limitations are shown.
Credit: pexels.com, Webpage of Ai Chatbot, a prototype AI Smith Open chatbot, is seen on the website of OpenAI, on a apple smartphone. Examples, capabilities, and limitations are shown.

The Nextjs metadata template is a powerful tool for optimizing your website's SEO and user experience. It allows you to customize the metadata for each page of your site, including the title, description, and keywords.

To get started with the Nextjs metadata template, you'll need to create a new file in the pages directory. This file will contain the metadata for your page, and will be used by Nextjs to generate the HTML head.

The metadata template is a simple JSON object that contains the metadata for your page. For example, if you have a page with the title "About Us" and the description "Learn more about our company", your metadata template might look like this: { "title": "About Us", "description": "Learn more about our company" }.

Next.js Metadata Template

In Next.js, you can define a template object to add flexibility to your title field. This template object can be used to add a prefix or a suffix to titles defined in child route segments.

Recommended read: Next Js Backend

Credit: youtube.com, Next.js 14 Tutorial - 18 - title Metadata

The template object has three properties: template, default, and absolute. The template property allows you to add a prefix or suffix to titles, while the default property is used as a fallback title for child route segments that don't define a title. The absolute property, if defined, overrides the template set in parent segments.

You can define the title field using a template object in files like layout.tsx or page.tsx. For example, you can define the title as follows:

  • Use the template property to add a prefix or suffix to titles.
  • Use the default property as a fallback title for child route segments.
  • Use the absolute property to override the template set in parent segments.

To expand the metadata object, you can add the openGraph and twitter fields. These fields hold metadata specifically intended for social media platforms.

Here are the properties you can use to define OpenGraph and Twitter fields:

By setting up metadata for Open Graph and Twitter, you can trigger dynamic image generation and observe the preview in action when sharing the page's link on social media platforms.

OpenGraph and Twitter Fields

OpenGraph and Twitter fields are used to hold metadata specifically intended for social media platforms.

Credit: youtube.com, Getting Started with NextJS Dynamic OpenGraph Images

We can add these fields to the metadata object to enhance the page's presence on social media.

To set up metadata for OpenGraph, we invoke the /api/og endpoint to trigger dynamic image generation.

The metadata object will look like this:

The page's link can be shared on Twitter or Facebook to observe the preview in action.

We can pass any parameter we want to the /api/og endpoint, not just the page's title.

Related reading: Why Is Metadata Important

Routing Configuration

Routing Configuration is crucial for providing relevant metadata to individual pages in your Next.js application.

The default metadata object in the layout.tsx file, created by create-next-app, has a title property set to "Create Next App" and a description property set to “Generated by create next app”. This metadata is not suitable for individual pages, as it remains the same regardless of the visited route.

To improve SEO benefits, you'll need to configure routing for each page to provide unique metadata. The metadata object should contain a title property and a description property that accurately reflect the content of each page. This will allow search engines to better understand the structure and content of your application.

You might like: Next Js App vs Pages

Static and Dynamic Content

Credit: youtube.com, Metadata in Next.js | Static vs Dynamic Metadata in Next.js | Next.js 14 Tutorial -19

Static metadata is defined by exporting a Metadata object from a layout.tsx or page.tsx file. This is where you can set default metadata that will be applied to all pages in your Next.js application.

To get started, simply export a Metadata object in your app/page.tsx file, like this:

Static metadata is useful for setting metadata that doesn't need to change depending on the page's content. For example, you can set a default title or description that will be applied to all pages.

Dynamic metadata, on the other hand, is generated based on the page's content. This is particularly useful for setting SEO-related tags that change depending on the page's content. You can create dynamic metadata using Next.js's metadata API in your page.js file.

You can also define static metadata at the layout level, which provides a default for all pages. This is done by exporting a metadata object in a layout file. Static metadata in layout.js provides a default for all pages, and the dynamic metadata in page.js overrides it for specific routes as needed.

Recommended read: Nextjs Dynamic

Template Structure

Credit: youtube.com, Next.js 14 - SEO & Metadata Tutorial (Complete Guide)

In Next.js, you can define the title field using a template object, which provides more flexibility.

The template property in the title object can be used to add a prefix or a suffix to titles defined in child route segments.

You can use the default property as a fallback title for child route segments that don't define a title.

The absolute property, if defined, overrides the template set in parent segments.

Here's a breakdown of the template structure:

API and Sitemap

In Next.js, you can inform search engines about all pages of your site using a sitemap, which can be particularly useful if you have a multilingual or region-specific website.

You can attach locale-specific alternate entries to every URL in the sitemap to indicate that a particular page is available in multiple languages or regions. This is especially helpful if you're using the next-intl feature to return the link response header.

This way, you can provide more specific information to search engines about your localized pages, which can improve their visibility in search results.

You might like: Using State in Next Js

API

Credit: youtube.com, Intro to Search Console APIs - Google Search Console Training

APIs play a crucial role in making our websites more accessible and user-friendly. Metadata API is a great example of this, allowing us to internationalize metadata like page titles using functionality from next-intl.

The generateMetadata function can be exported from pages and layouts to achieve this. By passing an explicit locale to the awaitable functions from next-intl, we can make the metadata handler eligible for static rendering.

This is particularly useful if you're using i18n routing. It's a simple yet effective way to improve the user experience on your website.

Sitemap

Sitemap is a crucial tool for informing search engines about all pages of your site. If you're using a sitemap, you can attach locale-specific alternate entries to every URL to indicate that a particular page is available in multiple languages or regions.

By default, next-intl returns the link response header to instruct search engines about multiple languages. However, you can also provide this information in a sitemap for more specific requirements.

Next.js supports providing alternate URLs per language via the alternates entry as of version 14.2. You can use your default locale for the main URL and provide alternate URLs based on all locales that your app supports.

The default locale should also be included in the alternates object.

A unique perspective: Title of Specific Page Nextjs

Glen Hackett

Writer

Glen Hackett is a skilled writer with a passion for crafting informative and engaging content. With a keen eye for detail and a knack for breaking down complex topics, Glen has established himself as a trusted voice in the tech industry. His writing expertise spans a range of subjects, including Azure Certifications, where he has developed a comprehensive understanding of the platform and its various applications.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.