src/pages/README.md
This directory contains SEO generation functions for Next.js Pages Router applications.
Note: These functions are specifically for Pages Router. For App Router applications, use the main next-seo package exports instead.
npm install next-seo
# or
yarn add next-seo
# or
pnpm add next-seo
Import from next-seo/pages instead of next-seo:
import { generateNextSeo, generateDefaultSeo } from "next-seo/pages";
Important: In Pages Router, you need to wrap the generated SEO tags with Next.js's
<Head>component to ensure the meta tags are properly rendered in the document head.
Add SEO meta tags to individual pages:
// pages/about.tsx
import Head from "next/head";
import { generateNextSeo } from "next-seo/pages";
export default function AboutPage() {
return (
<>
<Head>
{generateNextSeo({
title: "About Us",
description: "Learn more about our company",
canonical: "https://example.com/about",
openGraph: {
url: "https://example.com/about",
title: "About Us",
description: "Learn more about our company",
images: [
{
url: "https://example.com/og-image.jpg",
width: 800,
height: 600,
alt: "About Us",
},
],
},
})}
</Head>
<h1>About Us</h1>
</>
);
}
Set global SEO defaults in your _app.tsx:
// pages/_app.tsx
import type { AppProps } from "next/app";
import Head from "next/head";
import { generateDefaultSeo } from "next-seo/pages";
const DEFAULT_SEO = {
titleTemplate: "MySite | %s",
defaultTitle: "MySite",
description: "Welcome to MySite",
openGraph: {
type: "website",
locale: "en_US",
url: "https://example.com/",
siteName: "MySite",
},
twitter: {
handle: "@mysite",
site: "@mysite",
cardType: "summary_large_image",
},
};
export default function MyApp({ Component, pageProps }: AppProps) {
return (
<>
<Head>{generateDefaultSeo(DEFAULT_SEO)}</Head>
<Component {...pageProps} />
</>
);
}
title - Page titletitleTemplate - Title template for all pages (use %s for title placeholder)defaultTitle - Default title when no title is setdescription - Page descriptioncanonical - Canonical URLthemeColor - Theme color meta tagnoindex - Set to true to noindex the pagenofollow - Set to true to nofollow the pagerobotsProps - Additional robots meta propertiesopenGraph={{
type: 'website',
url: 'https://example.com',
title: 'Open Graph Title',
description: 'Open Graph Description',
images: [{
url: 'https://example.com/og.jpg',
width: 800,
height: 600,
alt: 'Og Image Alt',
}],
siteName: 'SiteName',
locale: 'en_US',
}}
twitter={{
handle: '@handle',
site: '@site',
cardType: 'summary_large_image',
}}
additionalMetaTags={[
{
property: 'dc:creator',
content: 'Jane Doe'
},
{
name: 'application-name',
content: 'NextSeo'
}
]}
additionalLinkTags={[
{
rel: 'icon',
href: 'https://example.com/favicon.ico',
},
{
rel: 'apple-touch-icon',
href: 'https://example.com/apple-touch-icon.png',
sizes: '76x76'
}
]}
If you're using the old import path or component-based approach, update your code:
// Old (deprecated components)
import { NextSeo, DefaultSeo } from "next-seo";
// Usage: <NextSeo title="..." />
// New (Pages Router functions)
import { generateNextSeo, generateDefaultSeo } from "next-seo/pages";
// Usage: {generateNextSeo({ title: "..." })}
All types are exported from next-seo/pages:
import type { NextSeoProps, DefaultSeoProps } from "next-seo/pages";
For complete documentation and advanced usage, please refer to the main next-seo documentation.