Fixing Next.js Issue: Duplicate Meta Description and Tags

Updated: January 1, 2024 By: Guest Contributor Post a comment

Understanding the Problem

The error concerning duplicate meta description and tags in Next.js typically occurs when there is repetitive code defining meta tags within various components of your application or when unintentionally rendering the same component multiple times. In Next.js 11 or newer, the built-in <Head> component might be used in different parts of the application to specify the same meta tags, leading to clashes and duplication issues.

Solutions

Identifying the Source of Duplication

To rectify the issue, first identify the components where the meta tags are being defined. Oftentimes, the meta tags are specified within pages or global layout components, and double-checking their usage will help pinpoint where the duplications occur.

Consolidating Meta Tags

Once you have identified the problematic areas, aim to consolidate your meta tags into a single place, preferably in a custom document or a main layout component which is common across different pages. The custom document is the ideal location for global meta tags that should be replicated on all site pages.

Implementing Custom Document with Meta Tags

Create a new file _document.js or _document.tsx if you are using TypeScript within your pages directory. In this file, extend the default Document and override the render method to include your meta tags.


  import { Html, Head, Main, NextScript } from 'next/document';

  class MyDocument extends Document {
    render() {
      return (
        <Html>
          <Head>
            <meta name='description' content='Your website description' />
            // ... other meta tags
          </Head>
          <body>
            <Main />
            <NextScript />
          </body>
        </Html>
      );
    }
  }

  export default MyDocument;

Be aware that meta tags in the custom document apply site-wide. For page-specific meta tags, remove them from individual pages and manage them through a central state or a higher-order component that handles document metadata.

Using a Centralized State or Meta Management

To manage page-specific meta tags, consider using a central state or a dedicated meta management component. React Context or a state management library can hold the metadata, and a React Hook can retrieve this data from the context and apply it to the <Head> component present in your page’s layout.

Creating a Meta-Management Component

Create a component that applies the meta tags you require. For instance:


  import { useRouter } from 'next/router';
  import Head from 'next/head';

  const MetaManager = ({ pageTitle, pageDescription }) => {
    const router = useRouter();

    return (
      <Head>
        <title>{pageTitle} - MySite</title>
        <meta name="description" content={pageDescription} />
        <link rel="canonical" href={`https://www.mysite.com${router.asPath}`} />
        // ... other meta tags unique to the page
      </Head>
    );
  };

  export default MetaManager;

Use this component at the top of your page components, passing in the relevant information for each particular page.

Final Tips

After setting up your centralized meta tag system, double-check your application for stray <Head> instances that might cause duplicates. It’s also a good idea to leverage SEO testing tools to scan for any potential meta tag issues. Henceforth, all meta information should flow through your chosen centralized system, reducing the likelihood of future duplicates and keeping your application SEO-friendly.

In Conclusion

Fixing duplicate meta description and tags in Next.js involves identifying where the duplications occur, consolidating meta tags to a single location, implementing a custom document for sitewide tags, and potentially using a state or context for dynamic page-specific meta tags. Keep your application clean, organized, and SEO-optimized by managing your metadata efficiently.