DEV Community

Cover image for Boosting Your Next.js App with SEO: Implementing Static & Dynamic Metadata
Amr Saafan for Nile Bits

Posted on • Originally published at nilebits.com

Boosting Your Next.js App with SEO: Implementing Static & Dynamic Metadata

It is imperative that your Next.js application is search engine optimized in the cutthroat online environment of today. Though Next.js provides powerful server-side rendering and static site creation tools, the real strength is in how you use and maintain both static and dynamic information to improve your app's search engine ranking. With the help of this thorough tutorial, you will be able to optimize your Next.js application and increase its search engine ranking and reach by using both static and dynamic information.

Understanding Metadata in Next.js

Understanding what metadata is and why it's important for SEO is necessary before delving into the code. Search engines index and rank your web pages based on metadata, which includes things like the title, description, and keywords. The visibility of your website can be considerably increased with well-managed metadata.

Setting Up a Next.js Project

Let's start by creating a new Next.js project. If you haven't already set up Next.js, follow these steps to get started:

npx create-next-app my-seo-app
cd my-seo-app
npm run dev
Enter fullscreen mode Exit fullscreen mode

This will create a basic Next.js application that we will use to implement SEO best practices.

Implementing Static Metadata

Static metadata is content that doesn't change and is set at the build time. In Next.js , static metadata can be implemented using the

component. Here's an example of how to add static metadata to a page:
import Head from 'next/head';

export default function Home() {
  return (
    <>
      <Head>
        <title>My SEO-Optimized Next.js App</title>
        <meta name="description" content="This is a sample application optimized for SEO using Next.js." />
        <meta name="keywords" content="Next.js, SEO, Static Metadata" />
        <meta name="robots" content="index, follow" />
      </Head>
      <h1>Welcome to My SEO-Optimized Next.js App</h1>
      <p>This is the homepage of your SEO-friendly Next.js application.</p>
    </>
  );
}
Enter fullscreen mode Exit fullscreen mode

Implementing Dynamic Metadata

Dynamic metadata, on the other hand, changes based on the content or user interaction. This is particularly useful for pages like blogs or product listings, where each page might have different metadata. Next.js makes it easy to generate dynamic metadata by fetching data during the server-side rendering process.

Here’s how you can implement dynamic metadata in a Next.js app:

import Head from 'next/head';

export async function getServerSideProps(context) {
  const { slug } = context.params;
  const res = await fetch(`https://api.example.com/posts/${slug}`);
  const post = await res.json();

  return {
    props: {
      post,
    },
  };
}

export default function BlogPost({ post }) {
  return (
    <>
      <Head>
        <title>{post.title} - My Blog</title>
        <meta name="description" content={post.excerpt} />
        <meta name="keywords" content={post.keywords.join(', ')} />
        <meta name="robots" content="index, follow" />
      </Head>
      <h1>{post.title}</h1>
      <p>{post.content}</p>
    </>
  );
}
Enter fullscreen mode Exit fullscreen mode

Combining Static and Dynamic Metadata

In many cases, you might want to combine both static and dynamic metadata. For instance, you could have a static base title for your site but dynamically generate other metadata based on the content. Here's an example:

import Head from 'next/head';

export async function getStaticProps() {
  const res = await fetch('https://api.example.com/homepage');
  const data = await res.json();

  return {
    props: {
      data,
    },
  };
}

export default function Home({ data }) {
  return (
    <>
      <Head>
        <title>{data.title} - My SEO-Optimized Next.js App</title>
        <meta name="description" content={data.description} />
        <meta name="keywords" content={data.keywords.join(', ')} />
      </Head>
      <h1>{data.title}</h1>
      <p>{data.content}</p>
    </>
  );
}
Enter fullscreen mode Exit fullscreen mode

Advanced SEO Techniques with Next.js

Beyond basic metadata management, Next.js offers advanced features to enhance your SEO strategy. Here are a few techniques:

Canonical Tags: Prevent duplicate content issues by specifying canonical URLs for your pages.

<Head>
  <link rel="canonical" href="https://example.com/your-page" />
</Head>
Enter fullscreen mode Exit fullscreen mode

Open Graph and Twitter Cards: Improve social media sharing by adding Open Graph and Twitter Card metadata.

<Head>
  <meta property="og:title" content="My SEO-Optimized Next.js App" />
  <meta property="og:description" content="This is a sample application optimized for SEO using Next.js." />
  <meta property="og:image" content="https://example.com/og-image.jpg" />
  <meta property="og:url" content="https://example.com" />
  <meta name="twitter:card" content="summary_large_image" />
</Head>
Enter fullscreen mode Exit fullscreen mode

Structured Data: Implement JSON-LD structured data to help search engines better understand your content.

<Head>
  <script type="application/ld+json">
    {`
      {
        "@context": "https://schema.org",
        "@type": "WebSite",
        "url": "https://example.com",
        "name": "My SEO-Optimized Next.js App",
        "potentialAction": {
          "@type": "SearchAction",
          "target": "https://example.com/search?q={search_term_string}",
          "query-input": "required name=search_term_string"
        }
      }
    `}
  </script>
</Head>
Enter fullscreen mode Exit fullscreen mode

Optimizing Performance for SEO

Performance is a critical factor in SEO. Search engines like Google prioritize fast-loading websites, and Next.js provides several features to enhance performance:

Image Optimization: Use the next/image component for optimized image loading.

Code Splitting: Next.js automatically splits your code, loading only what’s necessary.

Static Site Generation (SSG): Where possible, use SSG to serve pre-rendered pages for faster load times.

import Image from 'next/image';

export default function Home() {
  return (
    <>
      <Head>
        <title>Optimized Images in Next.js</title>
      </Head>
      <h1>Optimized Images</h1>
      <Image
        src="/images/sample.jpg"
        alt="Sample Image"
        width={500}
        height={500}
      />
    </>
  );
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Implementing SEO in your Next.js app is not just about adding meta tags. It involves a holistic approach, combining static and dynamic metadata, optimizing performance, and leveraging advanced features like structured data and Open Graph. By following this guide, you’ll be well on your way to ensuring your Next.js application is not only fast and functional but also ranks well on search engines.

References:

Next.js Documentation

Google's SEO Starter Guide

Schema.org Structured Data

Open Graph Protocol

Twitter Cards

Top comments (0)