Creating a sitemap.xml

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

Professional NextJS Lessons

1.Course Intro
free
⏱️ 2:05
2.Get Setup
free
⏱️ 2:05
3.Project Template
free
⏱️ 5:45
4.Introducing FullStack React
free
⏱️ 4:57
5.App Router Reference
free
⏱️ 7:08
6.NextJS Server vs. "use client" Rendering
⏱️ 5:01
7.Nesting Server and Client Components
⏱️ 8:51
8.Client Only Components
⏱️ 3:48
9.Global CSS in NextJS
⏱️ 6:30
10.CSS Modules and SASS
⏱️ 4:15
11.Tailwind CSS
⏱️ 5:17
12.NextJS Custom Fonts
⏱️ 4:08
13.Static vs Dynamic Rendering
⏱️ 4:55
14.NextJS Link Component
⏱️ 3:20
15.NextJS Link Prefetching
⏱️ 5:49
16.Navigation Scroll with Link
⏱️ 2:02
17.Replace Navigation History
⏱️ 1:35
18.NextJS Image Component
⏱️ 7:01
19.Dynamic Image URLs
⏱️ 5:34
20.Safe Colocation with Private Folders
⏱️ 2:12
21.NextJS Route Groups
⏱️ 3:07
22.Dynamic Routes
⏱️ 5:37
23.NextJS API Routes
⏱️ 6:03
24.NextRequest and NextResponse
⏱️ 2:41
25.Cookies in API Handlers
⏱️ 4:25
26.React Suspense Simplified
⏱️ 2:23
27.Power of Suspense Boundaries
⏱️ 3:52
28.use Promises
⏱️ 3:24
29.Stream Promise Results from Server to Client
⏱️ 3:03
30.use Requires a Cached Promise
free
⏱️ 2:30
31.Suspense Error Boundaries
⏱️ 4:42
32.React Server Functions
⏱️ 4:50
33.Server Actions for Powerful Forms
⏱️ 5:35
34.useActionState to Simplify Form Actions
⏱️ 6:38
35.React Serializable Types
⏱️ 2:42
36.Create Dynamic Images with NextJS ImageResponse
⏱️ 5:54
37.Deploy NextJS Application to Production
⏱️ 3:51
38.NextJS Environment Variables Masterclass
⏱️ 3:50
39.Deploying and Managing Environment Variables
⏱️ 4:38
40.Using Databases for Dynamic Server Side State
free
⏱️ 7:34
41.Mastering NextJS Layout Files
⏱️ 4:54
42.Client State Management with NextJS
⏱️ 3:48
43.Client State Management In Action
⏱️ 8:04
44.Using Client State Management Libraries
⏱️ 5:25
45.Detecting Server vs Client Runtime and useEffect
⏱️ 4:33
46.Demystifying ISR vs SSR vs SSG
⏱️ 3:07
47.Static Site Generation or SSG with NextJS
⏱️ 3:16
48.Incremental Static Regeneration (ISR) Simplified
⏱️ 2:51
49.API Routes Caching with NextJS
⏱️ 3:05
50.Reference Guide to Caching for Modern NextJS
⏱️ 2:47
51.Setup a BASE_URL for client and server
free
⏱️ 2:54
52.Creating a sitemap.xml
⏱️ 3:29
53.Provide metadata with NextJS
⏱️ 6:54
54.Created High Quality Social Media Previews
⏱️ 7:38

Creating a sitemap.xml

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

Why Create a Sitemap?

Creating a sitemap can help search engines easily discover the content on your site, which can greatly improve your search ranking.

For example here is the sitemap for BooleanArt

Creating a Sitemap with NextJS

NextJs provides a simple way to generate a sitemap dynamically by using a known path for your code file which gets picked up by the NextJS Router.

Create file called sitemap.ts at the root of the app directory (you can use .js if you want).

As an example here is a sitemap that is generated using dynamic content from a CMS (content management system).

app/sitemap.ts
import type { MetadataRoute } from "next";

export const dynamic = 'force-dynamic';

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const response = await fetch(process.env.NEXT_PUBLIC_API_URL + "/products");
const { productNames } = (await response.json()) as {
productNames: string[];
};

return [
{
url: "",
priority: 1,
},
...productNames.map((name) => ({
url: `/product/${name}`,
priority: 0.8,
changeFrequency: "weekly" as const,
})),
].map((route: MetadataRoute.Sitemap[number]) => ({
...route,
url: "https://booleanart.com" + route.url,
}));
}

Beware of Caching

Sitemaps are more susceptible to caching because they rarely use dynamic inputs (like cookies, headers etc.). So to ensure you sitemap isn't cached by NextJS you can add a simple route config.

export const dynamic = 'force-dynamic';
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

Creating a site map can help search engines easily discover the content on your site, which can greatly improve your search ranking. Conventionally websites make available the site map at the route slash sitemap dot xml. For example, this is the site map for bullion art. Building a site map enable search engines to quickly and effectively locate the content on your site. Next year's provides a simple way to generate a site map dynamically by using a known path for your code file, which gets picked up by the next year's router. This file needs to be at the root of the app directory and it needs to be called sitemap.

00:31

You can use JavaScript or TypeScript. Hey, we are using TypeScript. We start off by importing the metadata route type from next vs. And then we export a default function that needs to return the metadata route site map. And you can even use an async function for your site map generator, which is why the return value over here is a promise to that site map. The site map is nothing more than an array of item. So we can start off by returning an FT array. And congratulations. If you now visit slash sitemap do XML. On our website, we now have a generated site map. It's conventional to have the first item.

01:04

It's just a website itself. And the URL over here will depend upon how you are going to deploy your site. Or alternatively, you can use the base URL, which is something that we covered in its own lesson. But to keep things simple, let's just use bullion art.com. Now this URL is actually going to be common across all the items that you add in the site map. So here's the neat trick. Leave this particular entry as empty and then map all of the items that you eventually plan to return. Modifying the URL and adding this as a prefix to the route URL. Note that we can use a lookup type, which is something that we covered in our TypeScript course

01:37

to get the type definitions of the site map item from the site map array. With these changes in place, our site map is no longer empty. We have one URL with a well-defined location. Since we are using an async function, we can make API calls and AWAI the result as well. For example, let's just load the list of product names from an external content management system. Note that this project contains dynamic routes for all the products. For example, slash product, apple product, orange product banana. And this project is an example that we built in a previous lesson. So given these product names, we can add them

02:10

to our site map with Stash product slash name. And with these product URLs added to our array, if you visit the site map in the browser, they show up over there as well. Beyond simply listing the URLs, you can even use the site map to inform the search engines of additional metadata. For example, the priorities of the different routes. When was the content on that route last modified, and how often do you expect that route to change? As a reference, here is the complete type definition for a site map item, and you can see that the only thing really required over here is the URL.

02:42

Let's play around with some of these properties. First up, let's take a look at priority. The default is 0.5, so let's bump up the priority of our site route all the way to the top, which is one. For our product pages, we still keep them high priority, but less than the root. So we use the value of 0.8. We expect our products to be changed every week. So we add a change frequency. And for the value use the string weekly. With these changes in place list, verify that the site map is working as expected. And no surprise, it looks fine with the new properties added. Note that the site map profile is caged by default. So if you have dynamic entries like this one,

03:16

a simple fix is to market as force dynamic. The exact values you use will depend upon your site and how you want to present it to the search engines, but at least now you have the tools to get the job done.

Professional NextJS

Professional NextJS

1.Course Intro
free
⏱️ 2:05
2.Get Setup
free
⏱️ 2:05
3.Project Template
free
⏱️ 5:45
4.Introducing FullStack React
free
⏱️ 4:57
5.App Router Reference
free
⏱️ 7:08
6.NextJS Server vs. "use client" Rendering
⏱️ 5:01
7.Nesting Server and Client Components
⏱️ 8:51
8.Client Only Components
⏱️ 3:48
9.Global CSS in NextJS
⏱️ 6:30
10.CSS Modules and SASS
⏱️ 4:15
11.Tailwind CSS
⏱️ 5:17
12.NextJS Custom Fonts
⏱️ 4:08
13.Static vs Dynamic Rendering
⏱️ 4:55
14.NextJS Link Component
⏱️ 3:20
15.NextJS Link Prefetching
⏱️ 5:49
16.Navigation Scroll with Link
⏱️ 2:02
17.Replace Navigation History
⏱️ 1:35
18.NextJS Image Component
⏱️ 7:01
19.Dynamic Image URLs
⏱️ 5:34
20.Safe Colocation with Private Folders
⏱️ 2:12
21.NextJS Route Groups
⏱️ 3:07
22.Dynamic Routes
⏱️ 5:37
23.NextJS API Routes
⏱️ 6:03
24.NextRequest and NextResponse
⏱️ 2:41
25.Cookies in API Handlers
⏱️ 4:25
26.React Suspense Simplified
⏱️ 2:23
27.Power of Suspense Boundaries
⏱️ 3:52
28.use Promises
⏱️ 3:24
29.Stream Promise Results from Server to Client
⏱️ 3:03
30.use Requires a Cached Promise
free
⏱️ 2:30
31.Suspense Error Boundaries
⏱️ 4:42
32.React Server Functions
⏱️ 4:50
33.Server Actions for Powerful Forms
⏱️ 5:35
34.useActionState to Simplify Form Actions
⏱️ 6:38
35.React Serializable Types
⏱️ 2:42
36.Create Dynamic Images with NextJS ImageResponse
⏱️ 5:54
37.Deploy NextJS Application to Production
⏱️ 3:51
38.NextJS Environment Variables Masterclass
⏱️ 3:50
39.Deploying and Managing Environment Variables
⏱️ 4:38
40.Using Databases for Dynamic Server Side State
free
⏱️ 7:34
41.Mastering NextJS Layout Files
⏱️ 4:54
42.Client State Management with NextJS
⏱️ 3:48
43.Client State Management In Action
⏱️ 8:04
44.Using Client State Management Libraries
⏱️ 5:25
45.Detecting Server vs Client Runtime and useEffect
⏱️ 4:33
46.Demystifying ISR vs SSR vs SSG
⏱️ 3:07
47.Static Site Generation or SSG with NextJS
⏱️ 3:16
48.Incremental Static Regeneration (ISR) Simplified
⏱️ 2:51
49.API Routes Caching with NextJS
⏱️ 3:05
50.Reference Guide to Caching for Modern NextJS
⏱️ 2:47
51.Setup a BASE_URL for client and server
free
⏱️ 2:54
52.Creating a sitemap.xml
⏱️ 3:29
53.Provide metadata with NextJS
⏱️ 6:54
54.Created High Quality Social Media Previews
⏱️ 7:38