Techtales.

Mastering SEO in NextJS

One major reason companies use Next.js in production is the built-in SEO features due to server-side rendering that allows all dynamic pages to be prebuilt, thus making them crawlable. 

Search Engine Optimization (SEO) can make or break your website’s online presence. With Next.js 15’s powerful new features, creating an SEO-optimized website has never been easier and has solved many issues. React faces, if you know what you’re doing.

In this guide, I will walk you through how I solved SEO optimization for this blog to ensure that each blog page has its own metadata. We’ll cover everything from basic metadata to advanced structured data, and I’ll show you exactly what code to write and why.

Understanding Next.js 15 SEO Foundations

The New Metadata API

Next.js 15 introduced a revolutionary way to handle SEO through the Metadata API. Gone are the days of manually managing <head> tags; now everything is handled through simple JavaScript objects.

Here’s the basic structure:

Why This Matters

The Metadata API isn’t just syntactic sugar; it provides several crucial advantages:

  1. Type Safety: TypeScript ensures you never miss required fields
  2. Automatic Optimization: Next.js handles meta tag placement and deduplication
  3. Dynamic Generation: Perfect for blogs, e-commerce, and content sites
  4. Template System: Consistent branding across all pages

Building a Bulletproof Root Layout

Your root layout (app/layout.tsx) is the foundation of your entire SEO strategy. Here's how to structure it properly:

The Critical Parts Explained

  1. metadataBase: Essential for relative URLs to work properly
  2. title. template: Automatically adds your brand to every page
  3. robots configuration: Fine-tuned control over search engine behavior
  4. OpenGraph + Twitter: Optimized social media sharing
  5. canonical: Prevents duplicate content issues

Structured Data: Speaking Google’s Language

Structured data is how you tell search engines exactly what your content means. It’s the difference between Google guessing what your business does and Google knowing what your business does.

Organization Schema

Every business website needs this basic schema:

Website Schema

This tells search engines about your site structure:

 

Dyamic SEO Metadata

One of the challenges you will face is now generating the full metadata for each dynamic route in Next.js. Dynamic routes such as /blog/[id] are used to generate static pages for every blog during build to make the site load faster for the user. 

Since each blog has a custom title and description, you cannot rely on the initial metadata in the layout folder, as this references the whole site. To solve this problem, first create a reusable metadata object. Here is a sample reusable metadata object:

For dynamic pages, we can use this metadata object as the base and only change some properties like title, author, description, keywords and images. Here is a custom function that I call during build (generateStaticParams) to create metadata for all blogs:

Generate Custom Metadata

Additionally, we need to create a JSON-LD script for each blog page. The way that we can do this is by appending the script into the blog page and computing the structure of the JSON during build time.

The last thing to consider is creating robots.txt and sitemaps. I often generate this automatically using the next-sitemap package. However, Next.js 15's app/sitemap.ts approach is newer and better and thus can be used to create sitemaps that are dynamic and respond to changes in the data. Since next-sitemap only builds the sitemap during build time, it does not update when new pages are added, for example, when users submit new blog posts for publication.

SEO Checklist for Launch

  • [✔] Google Search Console setup
  • [✔] Google Analytics installed
  • [✔] All images have descriptive alt tags
  • [✔] Every page has unique title <h1> and description
  • [✔] Sitemap submitted to search engines
  • [✔] Site speed under 3 seconds
  • [✔] Mobile-friendly design
  • [✔] HTTPS enabled
  • [✔] Internal linking strategy implemented

Conclusion

SEO in Next.js 15 isn’t just about adding meta tags; it’s about creating a comprehensive strategy that covers technical optimization, content structure, and user experience.

0
0