How to add a sitemap to a NextJS website

A tutorial for creating static or dynamic sitemaps in a NextJS project.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 10/09/2024 at 13:48
Last Updated on 10/09/2024 at 14:02
<p>A flower that represents NextJS with the text "Sitemap" beneath it.</p>

A tutorial for creating static or dynamic sitemaps in a NextJS project.

SubscribeDownload Open Source Project

The following post explains how to generate a sitemap using the NextJS app router.

If you are not familiar with the NextJS app router, consult the link below.

How the NextJS app router works

The tutorial follows the principles laid out in the official NextJS documentation linked below.

NextJS: Sitemap

In a file called sitemap.ts at the root of the app folder, do one of the following:

A/ Static Sitemap

<p>A screenshot of the NextJS website showing the documentation for creating a static sitemap.</p>

Add your URLs in a similar way to that described in the documentation.

NextJS: Sitemap

B/ Dynamic Sitemap

<p>A screenshot of the dynamic sitemap that we created for the student archive.</p>

To make a sitemap dynamic, you must export an asynchronous function that returns a promise.

async function Sitemap(): Promise&lt;MetadataRoute.Sitemap&gt;

Then within the function make the neccesary API calls and return an array similar to that illustrated in the documentation.

Looking to learn more about NextJS, ReactJS or web development?

Search our blog to find educational content on NextJS, ReactJS and web development.

Search our Blog

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe