Generate the metadata for a page
seo-in-nextjs can generate the metadata for a page in Next.js, including the title, description, canonical URL, and Open Graph and Twitter metadata.
Using the genPageMetadata function
Section titled “Using the genPageMetadata function”-
Import the
genPageMetadatafunction into every page where you want to generate metadata.app/page.tsx import { genPageMetadata } from "@dlcastillop/seo-in-nextjs";export const metadata = genPageMetadata({title: "Example - Your trusted source for quality examples",description:"Discover comprehensive examples and tutorials for web development, design patterns, and best practices.",pageRoute: "/",ogImg: "/og-home.png",}); -
Check the generated metadata by inspecting the head element of your page.
Using genPageMetadata in async pages
Section titled “Using genPageMetadata in async pages”When working with async pages, you can wrap genPageMetadata inside an async function to generate metadata dynamically based on fetched data.
-
Export an async
generateMetadatafunction that returns the result ofgenPageMetadata.app/blog/[slug]/page.tsx import { genPageMetadata } from "@dlcastillop/seo-in-nextjs";import type { Metadata } from "next";export async function generateMetadata(): Promise<Metadata> {return genPageMetadata;}app/blog/[slug]/page.jsx import { genPageMetadata } from "@dlcastillop/seo-in-nextjs";export async function generateMetadata() {return genPageMetadata;} -
Use the function parameters (like
paramsorsearchParams) to fetch the necessary data.app/blog/[slug]/page.tsx import { genPageMetadata } from "@dlcastillop/seo-in-nextjs";import type { Metadata } from "next";interface PageProps {params: Promise<{slug: string;}>;}async function getArticle(slug: string) {const res = await fetch(`https://api.example.com/articles/${slug}`);return res.json();}export async function generateMetadata({params,}: PageProps): Promise<Metadata> {const { slug } = await params;const article = await getArticle(slug);return genPageMetadata;}app/blog/[slug]/page.jsx import { genPageMetadata } from "@dlcastillop/seo-in-nextjs";async function getArticle(slug) {const res = await fetch(`https://api.example.com/articles/${slug}`);return res.json();}export async function generateMetadata({ params }) {const { slug } = await params;const article = await getArticle(slug);return genPageMetadata;} -
Pass the dynamic values to
genPageMetadata.app/blog/[slug]/page.tsx import { genPageMetadata } from "@dlcastillop/seo-in-nextjs";import type { Metadata } from "next";interface PageProps {params: Promise<{slug: string;}>;}async function getArticle(slug: string) {const res = await fetch(`https://api.example.com/articles/${slug}`);return res.json();}export async function generateMetadata({params,}: PageProps): Promise<Metadata> {const { slug } = await params;const article = await getArticle(slug);return genPageMetadata({title: `${article.title} - Example Blog`,description: article.description,pageRoute: `/blog/${slug}`,ogImg: article.coverImage,});}app/blog/[slug]/page.jsx import { genPageMetadata } from "@dlcastillop/seo-in-nextjs";async function getArticle() {const res = await fetch(`https://api.example.com/articles/${slug}`);return res.json();}export async function generateMetadata({ params }) {const { slug } = await params;const article = await getArticle(slug);return genPageMetadata({title: `${article.title} - Example Blog`,description: article.description,pageRoute: `/blog/${slug}`,ogImg: article.coverImage,});} -
Check the generated metadata by inspecting the head element of your page.
Related
Section titled “Related”View related API references.