Skip to content

    sitemap.xml

    Add or generate a sitemap.xml file that matches the Sitemaps XML format in the root of app directory to help search engine crawlers crawl your site more efficiently.

    Static sitemap.xml

    app/sitemap.xml
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
      <url>
        <loc>https://acme.com</loc>
        <lastmod>2023-04-06T15:02:24.021Z</lastmod>
      </url>
      <url>
        <loc>https://acme.com/about</loc>
        <lastmod>2023-04-06T15:02:24.021Z</lastmod>
      </url>
      <url>
        <loc>https://acme.com/blog</loc>
        <lastmod>2023-04-06T15:02:24.021Z</lastmod>
      </url>
    </urlset>

    Generate a Sitemap

    Add a sitemap.js or sitemap.ts file that returns Sitemap.

    app/sitemap.ts
    import { MetadataRoute } from 'next';
     
    export default function sitemap(): MetadataRoute.Sitemap {
      return [
        {
          url: 'https://acme.com',
          lastModified: new Date(),
        },
        {
          url: 'https://acme.com/about',
          lastModified: new Date(),
        },
        {
          url: 'https://acme.com/blog',
          lastModified: new Date(),
        },
      ];
    }

    Output:

    acme.com/sitemap.xml
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
      <url>
        <loc>https://acme.com</loc>
        <lastmod>2023-04-06T15:02:24.021Z</lastmod>
      </url>
      <url>
        <loc>https://acme.com/about</loc>
        <lastmod>2023-04-06T15:02:24.021Z</lastmod>
      </url>
      <url>
        <loc>https://acme.com/blog</loc>
        <lastmod>2023-04-06T15:02:24.021Z</lastmod>
      </url>
    </urlset>

    Sitemap Return Type

    type Sitemap = Array<{
      url: string;
      lastModified?: string | Date;
    }>;

    Good to know

    • In the future, we will support multiple sitemaps and sitemap indexes.

    Was this helpful?