For solo engineers, but not only.

How to Create a Dynamic Sitemap for Your Next.js 14 Blog: A Comprehensive Guide to Boost SEO

November 18, 2023Lev Gelfenbuim7 min. read

In the ever-evolving landscape of web development, maintaining a robust online presence is crucial for success. One integral aspect often overlooked is the creation of sitemap.xml, a powerful file that not only enhances search engine optimization (SEO) but also improves user experience by providing a clear and structured roadmap to your content.

As we delve into the intricacies of generating a dynamic sitemap.xml, we’ll focus on leveraging the capabilities of Next.js 14, a cutting-edge framework for building React applications. This blog post aims to guide you through the process of seamlessly integrating a dynamic sitemap into your Next.js 14-powered blog, ensuring your content remains easily discoverable by search engines.

We’ll explore the significance of sitemaps, the advantages of dynamic generation, and the step-by-step implementation using Next.js 14. By the end of this journey, you’ll be equipped with the knowledge to enhance your blog’s SEO standing and user engagement through the implementation of a well-structured and automatically updated sitemap.xml. Are you ready?

Understanding Sitemaps

A website’s success hinges not only on the quality of its content but also on how effectively that content can be discovered by search engines and navigated by users. This is where a sitemap.xml plays a pivotal role.

What is a Sitemap?

At its core, a sitemap is a file that provides a roadmap to search engines, guiding them through the structure of your website and highlighting essential pages. It’s essentially a blueprint that ensures search engine crawlers can index your content efficiently. Traditionally, sitemaps were static files manually updated whenever the website structure changed. However, in the dynamic landscape of modern web development, a more adaptive approach is necessary.

1<?xml version="1.0" encoding="UTF-8"?>
2<urlset xmlns="">
3  <url>
4    <loc></loc>
5    <lastmod>2023-11-01</lastmod>
6    <priority>0.8</priority>
7  </url>
8  <url>
9    <loc></loc>
10    <lastmod>2023-11-05</lastmod>
11    <priority>0.7</priority>
12  </url>
13  <url>
14    <loc></loc>
15    <lastmod>2023-11-10</lastmod>
16    <priority>0.6</priority>
17  </url>
18  <!-- ... -->
Importance of Dynamic Sitemaps

The term “dynamic” in the context of sitemaps refers to the ability to automatically update the sitemap at the runtime. For a blog, this means that every new post or updated page is seamlessly incorporated into the sitemap, ensuring that search engines are always aware of your latest content.

Why bother investing time in building a dynamic sitemap? Well, a dynamic sitemap ensures that new content is promptly indexed, boosting your site’s visibility in search engine results. Beyond SEO, a well-structured sitemap enhances the user experience by offering a clear and organized path through your website. Visitors can easily discover and access the content they’re interested in.

SEO Advantages of Sitemaps

Search engines use sitemaps as a reference to understand the structure of your website. A dynamic sitemap ensures that search engine crawlers efficiently index your content, improving the chances of it appearing in search results. Sitemaps allow you to specify the priority and update frequency of each page, providing valuable information to search engines about the importance of different sections of your site.

In the context of Next.js 14, where dynamic content rendering is a strength, the implementation of a dynamic sitemap becomes a strategic move!

Setting Up a Next.js 14 Project

Before we dive into the intricacies of generating a dynamic sitemap.xml for your Next.js 14 blog, let’s establish a solid foundation by setting up the project. Follow these steps to ensure a smooth development process:

1. Install Next.js 14

create-next-app is the easiest way to get you started.

1$ pnpx create-next-app@latest
3✔ What is your project named? … my-nextjs-blog
4✔ Would you like to use TypeScript? … Yes
5✔ Would you like to use ESLint? … Yes
6✔ Would you like to use Tailwind CSS? … Yes
7✔ Would you like to use `src/` directory? … Yes
8✔ Would you like to use App Router? (recommended) … Yes
9✔ Would you like to customize the default import alias (@/*)? … No

Let it finish and open the created directory in your favorite IDE.

Building the Sitemap Generation Logic

Now that we have a shiny, fresh Next.js 14 project, let’s create the sitemap file in src/app/sitemap.xml. For starters, let’s return two static URLs:

1import { MetadataRoute } from "next";
3export default function sitemap(): MetadataRoute.Sitemap {
4  return [
5    {
6      url: "",
7      priority: 1,
8    },
9    {
10      url: "",
11      changeFrequency: "weekly",
12      priority: 0.8,
13    },
14  ];

Next, spin up the development server by running: pnpm dev

And verify the sitemap is reachable: http://localhost:3000/sitemap.xml

If done right, you should see the following response appear:

2  <url>
3    <loc></loc>
4    <priority>1</priority>
5  </url>
6  <url>
7    <loc></loc>
8    <lastmod>2023-11-17T20:08:24.061Z</lastmod>
9    <changefreq>monthly</changefreq>
10    <priority>0.8</priority>
11  </url>

And now let’s do the actual action!

In the same file, create the following function:

1const generateBlogPostsSitemapObjects = async () => {
2  return [
3    {
4      slug: "blog-post-1",
5      updatedAt: new Date(),
6    },
7    {
8      slug: "blog-post-2",
9      updatedAt: new Date(),
10    },
11  ];

This TypeScript function, generateBlogPostsSitemapObjects, is a simple asynchronous function that generates an array of objects representing blog posts on a sitemap. You may ask why generateBlogPostsSitemapObjects is asynchronous? We’ll discuss this in a moment.

We would then need to tweak the sitemap() function a little:

1export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
2  return [
3    {
4      url: "",
5      priority: 1,
6    },
7    {
8      url: "",
9      changeFrequency: "weekly",
10      priority: 0.8,
11    },
12    ...(await generateBlogPostsSitemapObjects()).map((o) => ({
13      url: `${o.slug}`,
14      lastModified: o.updatedAt,
15    })),
16  ];

Note that the third object in the sitemap is generated dynamically by mapping over the array of blog post objects returned by generateBlogPostsSitemapObjects. It creates an object for each blog post with a URL in the format${o.slug} and sets the last modified date to o.updatedAt. The use of the spread operator (...) is employed to concatenate the array returned by generateBlogPostsSitemapObjects with the static sitemap objects.

Assuming you are working with an external CMS, you would have to asynchronously call its API to obtain a list of blog posts. For example, if we were to implement Sanity CMS, we would use a GROQ query similar to:

1*[_type == "post" && !(_id in path("drafts.**"))] | order(_createdAt desc) {
2    "slug": slug.current,
3    "createdAt": _createdAt,
4    "updatedAt": _updatedAt

This query would return all blog slugs, creation and modification dates that are not drafts.

Visit http://localhost:3000/sitemap.xml once again. You should see the following response:

2  <url>
3    <loc></loc>
4    <priority>1</priority>
5  </url>
6  <url>
7    <loc></loc>
8    <changefreq>weekly</changefreq>
9    <priority>0.8</priority>
10  </url>
11  <url>
12    <loc></loc>
13    <lastmod>2023-11-17T20:35:34.180Z</lastmod>
14  </url>
15  <url>
16    <loc></loc>
17    <lastmod>2023-11-17T20:35:34.180Z</lastmod>
18  </url>

Every time a search engine crawler visits your sitemap.xml file, it will be served with a fresh response that contains all your published blog posts!


In the ever-evolving landscape of web development, the journey to optimize your online presence is an ongoing one. In this exploration, we delved into the realm of dynamic sitemaps for a blog, unraveling the significance of this often-underestimated component.

As we’ve seen, a dynamic sitemap.xml is not merely a technical necessity but a strategic asset that propels your blog’s visibility on search engines. The integration of a well-structured and automatically updated sitemap seriously helps your content remain at the forefront of search engine rankings.

By navigating through the intricacies of setting up a Next.js 14 project, and implementing the logic for sitemap generation, you’ve equipped yourself with the tools to propel your blog into the digital spotlight. Regularly updating your sitemap ensures that search engines promptly index your latest content, contributing to sustained SEO success.

As you continue refining your blog and exploring further avenues for improvement, remember that the dynamic sitemap is not just a technical detail but a strategic investment in the discoverability and accessibility of your content. Embrace the dynamic sitemap as a companion on your journey towards an optimized and user-friendly Next.js 14 blog, and let it guide both search engines and users seamlessly through the valuable content you offer. 🚀

Article last update: February 12, 2024