---
title: "Configure sitemap.xml using a Route Handler"
slug: "configure-sitemap-xml-using-a-route-handler"
abstract: "Learn how to configure and serve sitemap.xml (single or index) in a Content SDK Next.js App Router template, including caching controls and multisite host resolution."
---
# Configure sitemap.xml using a Route Handler

In the Content SDK Next.js App Router template, `sitemap.xml` is delivered by a Route Handler rather than a physical file. This approach has the following benefits:

- Per-site resolution (multisite support) based on the incoming `Host` header.
- Dynamic selection between a single sitemap or an index sitemap.
- Controlled caching and revalidation of responses.

> [!note]
> Before you begin, see how you can [configure the sitemap.xml file](https://doc.sitecore.com/xmc/en/developers/xm-cloud/en/configure-a-sitemap-in-the-content-editor.html).

## Handler implementation

A dedicated handler created with `createSitemapRouteHandler` responds at `/api/sitemap`, and a Next.js rewrite exposes it at the canonical `/sitemap.xml` path. The route handler can emit either a single sitemap (list of URLs) or an index sitemap referencing multiple sitemap files. You can use the `createSitemapRouteHandler` method from `@sitecore-content-sdk/nextjs/route-handler` to scaffold the `GET` implementation. It requires the following:

- A Sitecore client instance
- A list of sites (`SiteInfo`) (commonly imported from `.sitecore/sites.json`)

See the following example implementation of the handler:

```ts
import { createSitemapRouteHandler } from '@sitecore-content-sdk/nextjs/route-handler';
import sites from '.sitecore/sites.json';
import client from 'lib/sitecore-client';

export const { GET } = createSitemapRouteHandler({
  client,
  sites,
});
```

Add this to `app/api/sitemap/route.ts` and define a Next.js rewrite in `next.config.ts`:

```ts
const nextConfig: NextConfig = {
  ...
  rewrites: async () => {
    return [
      {
        source: '/sitemap:id([\\w-]{0,}).xml',
        destination: '/api/sitemap',
        locale: false,
      },
      ...
    ];
  },
};
```

## Caching and revalidation

By default, responses are cached for 60 seconds. To adjust or disable revalidation:

- Pass a number (seconds) to set a new duration.
- Pass `false` to skip revalidation and cache indefinitely (only do this if content is truly static).
- Omit `revalidate` to use the default.

See the following example with a custom duration:

```ts
export const { GET } = createSitemapRouteHandler({
  client,
  sites,
  revalidate: 90, // cache for 90 seconds
});
```