Sling Academy
Home/Next.js/Fixing Next.js Issue: Duplicate Meta Description and Tags

Fixing Next.js Issue: Duplicate Meta Description and Tags

Last updated: January 01, 2024

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.

Next Article: Fixing Next.js Error: Blank Page or 404 on Refresh

Previous Article: Next-Auth Issue: User Must Login Twice to Get Session – Fixes Explained

Series: Common Errors in Next.js

Next.js

Related Articles

You May Also Like

  • Solving Next.js Image Component Error with CSS Classes
  • How to Use MUI (Material UI) in Next.js 14
  • Fixing Data Update Issues in Next.js Production
  • Fixing Next.js Undefined ENV Variables in Production Build
  • Solving Next.js SyntaxError: Unexpected token ‘export’
  • Next.js Error: Found Page Without a React Component as Default Export – How to Fix
  • Fixing Next.js Error: Blank Page on Production Build
  • Fixing Next.js Error when Importing SVGs: A Step-by-Step Guide
  • Next.js Issue: useEffect Hook Running Twice in Client
  • Fixing ‘self’ is not defined error in Next.js when importing client-side libraries
  • How to Dockerize a Next.js App for Production
  • How to set up Next.js with Docker Composer and Nginx
  • Solving Next.js CORS Issues: A Comprehensive Guide
  • Fixing Next.js Hydration Mismatch Error in Simple Ways
  • Next.js Error: Cannot use import statement outside a module
  • Next.js: How to Access Files in the Public Folder
  • Fixing Next.js Import CSS Error: A Simple Guide
  • Fixing LocalStorage Not Defined Error in Next.js
  • Fixing Next.js Error: No HTTP Methods Exported