17 SEO with Next.js

Search engine optimization (SEO) is an essential part of building modern web applications. Next.js offers several features to improve the SEO friendliness of applications. These features enable better indexing by search engines and an improved user experience.

17.1 Server-side rendering (SSR)

Next.js supports server-side rendering by default, which means that the content of a page is generated by the server when it is first loaded. This is particularly advantageous for SEO, as search engines can capture server-side rendered content more easily.

17.2 Static page generation (SSG)

Next.js also enables the static generation of pages with getStaticProps and getStaticPaths. Static pages are generated at build time and can be indexed immediately by search engines. This improves loading times and the availability of content for search engine crawlers.

17.3 Dynamic routes

Next.js supports dynamic routes that allow you to create SEO-friendly URLs for different content. With dynamic routes, you can insert parameters into URLs that contain information relevant to search engines.

17.4 Meta tags and head management

Next.js provides an easy way to define meta tags for each page with the <head> component. This is crucial for SEO, as meta tags such as title, description and keywords are used by search engines to evaluate and rank the page.

17.4.1 Example:

import Head from 'next/head';

function MyPage() {
  return (
    <>
      <head>
        <title>My SEO-optimized page</title>
        <meta name="description" content="Description of my page" />
        <meta name="keywords" content="Keywords, SEO" />
        // Further meta tags
      </head>
      // Content of the page
    </>
  );
}

17.5 Canonical tags

Canonical tags can be easily inserted in Next.js via the <head> components. These tags help to avoid duplicate content issues by telling search engines the preferred URL of a page.

17.6 Accessibility and Semantic HTML

Next.js promotes the use of semantic HTML and accessibility practices that also contribute to SEO. Accessible websites are often ranked higher by search engines because they provide a better user experience.

17.7 Exercise: SEO-optimized blog application with Next.js

17.7.1 Task

Create a simple, SEO-optimized blog application with Next.js. The application should fulfill the following requirements:

  1. Static pages: Each blog post should be generated as a static page.
  2. Dynamic routes: Use dynamic routes for blog posts.
  3. SEO elements: Each blog post should have individual meta tags for title and description.
  4. List of all blog posts: Create a main page that contains links to all blogposts.

17.7.2 Structure of the application

17.8 Creating sitemaps in Next.js

Sitemaps are an important instrument in the SEO toolkit as they help search engines to understand the structure of a website and crawl all its pages efficiently. In Next.js, sitemaps can be created manually or automatically.

17.8.1 1. basic concept of a sitemap

A sitemap is an XML file that contains a list of URLs of a website. It provides search engines with important metadata about each URL, such as the date of the last update, the frequency of changes and the relative importance compared to other URLs on the site.

17.8.2 2. manual creation of a sitemap

For smaller projects or websites with few pages, a sitemap can be created manually as an XML file and stored in the public directory (public). For example, a simple sitemap could look like this:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://www.beispiel.de/</loc>
    <lastmod>2023-01-01</lastmod>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>https://www.beispiel.de/seite-2</loc>
    <lastmod>2023-01-01</lastmod>
    <priority>0.8</priority>
  </url>
  // Further URLs
</urlset>

17.8.3 3. automated sitemap generation

For dynamic or large websites, it is advisable to generate sitemaps automatically. This can be achieved using scripts that run during the build process. These scripts can extract all the necessary URLs from your data sources (e.g. a database or files in the file system) and generate a sitemap XML file.

17.8.3.1 Example script:

A Node.js script could look like this:

const fs = require('fs');
const { SitemapStream, streamToPromise } = require('sitemap');
const { Readable } = require('stream');

// List of your URLs
const links = [{ url: '/', changefreq: 'daily', priority: 1 }];

// Create sitemap stream
const sitemapStream = new SitemapStream({ hostname: 'https://www.beispiel.de' });

streamToPromise(Readable.from(links).pipe(sitemapStream)).then((data) =>
  fs.writeFileSync('./public/sitemap.xml', data)
);

17.8.4 4. send sitemap to search engines

Once the sitemap is created and available on your website, you should send the URL of the sitemap to search engines such as Google via their webmaster tools. This ensures that the search engine is aware of the sitemap and crawls your pages efficiently.

17.9 Exercise: Creating an automated sitemap in Next.js

17.9.1 Task

Develop a Next.js application that automatically generates a sitemap for your pages. The application should contain at least three static pages (e.g. Home, About us, Contact). Implement a script that generates a sitemap of these pages during the build process of the application and stores it in the public directory.

17.9.2 Requirements

  1. Initialize a Next.js application. 2 Create at least three static pages** (e.g. pages/index.js, pages/about-us.js, pages/contact.js). 3 Implement a Node.js script that creates a sitemap of these pages. 4 The script should save the sitemap XML in the public directory. 5 Add the script to the build process** so that the sitemap is updated with each build.

17.10 Canonical and other meta tags in Next.js

The use of canonical links and other meta tags is an important aspect of SEO strategy. In Next.js, these tags can be easily integrated into your web pages to help search engines index and display your content correctly.

17.10.1 Canonical tags

Canonical tags are used to tell search engines the preferred URL of a page, especially when similar or identical content is available under different URLs. This helps to avoid problems with duplicate content.

17.10.1.1 Example:

import Head from 'next/head';

function MyPage() {
  return (
    <>
      <head>
        <link rel="canonical" href="https://www.beispiel.de/meine-seite" />
      </head>
      // Content of the page
    </>
  );
}

17.10.2 Other important meta tags

1st Title Tag: Specifies the title of the page. Important for search engines and user-friendliness. jsx <title>Page title</title>

  1. Description meta tag: Describes the content of the page. Is often displayed in search results.

    <meta name="description" content="Description of the page" />

3 Robots meta tag: Controls how search engines crawl and index the page. jsx <meta name="robots" content="index, follow" />

  1. Viewport meta tag: Important for the responsive design of the website.

    <meta name="viewport" content="width=device-width, initial-scale=1" />

5 Open Graph Tags (OG Tags): Enable better display of the page on social networks when shared. jsx <meta property="og:title" content="Page title" /> <meta property="og:description" content="Description of the page" /> <meta property="og:image" content="URL to the image" />

6th Twitter Cards: Similar to OG tags, specifically for sharing on Twitter. jsx <meta name="twitter:card" content="summary_large_image" />

17.10.3 Implementation in Next.js

In Next.js, these tags can be easily added with the <head> components on each page or in a layout component for the entire application. This allows you to customize SEO-relevant information at the page level to maximize the visibility and effectiveness of your website in search engines.

17.11 Exercise: Implementation of SEO optimizations in a Next.js app

17.11.1 Objective

Create a simple Next.js application that demonstrates various SEO techniques, including the use of meta tags, canonical tags and open graph tags.

17.11.2 Requirements

1st Project Setup: Create a new Next.js project. 2. Page Creation: Create two pages (index.js and about.js). 3. SEO component: Implement a reusable component for SEO tags. 4. Meta tags: Add specific meta tags on both pages. 5. Canonical Tag: Implement canonical tags for each page. 6. Open Graph Tags: Add OG tags to optimize the pages for social media sharing.

17.12 Core Web Vitals in Next.js

Core Web Vitals are a set of specific factors that Google considers as part of its “Page Experience” metrics. These metrics measure the user experience when loading, interacting and navigating a website. In Next.js, developers can optimize these metrics to improve SEO performance.

17.12.1 The three main metrics of Core Web Vitals

  1. Largest Contentful Paint (LCP): Measures the load time of the largest visible content on the page. The goal is to reach this within 2.5 seconds of the page load starting.

  2. First Input Delay (FID): Measures the time from a user’s first interaction (e.g. a click) until the page responds. A good FID value is less than 100 milliseconds.

  3. Cumulative Layout Shift (CLS): Measures the visual stability of the page. The aim is to achieve a CLS value of less than 0.1 to prevent content from shifting unexpectedly when the page is loaded.

17.12.2 Optimization in Next.js

17.12.2.1 Optimization of the Largest Contentful Paint (LCP)

17.12.2.2 Reduction of the First Input Delay (FID)

17.12.2.3 Minimize the Cumulative Layout Shift (CLS)

17.12.3 Measurement of the Core Web Vitals

17.13 Exercise: Optimization of the Core Web Vitals in a Next.js application

17.13.1 Task

Create a simple Next.js application that focuses on optimizing the Core Web Vitals. The application should fulfill the following criteria:

  1. Page with images: Integrate a page that loads multiple images. Use the <Image /> component of Next.js for image optimization.

2 Dynamic loading of components: Implement a function where a component (e.g. a widget or an information card) is dynamically loaded after a user clicks a button.

3 Measure Core Web Vitals: Add a tool to measure the Core Web Vitals of your application. You can use Lighthouse in Chrome DevTools for this.