This is a pretty straightforward example of creating a sitemap.xml page on your SvelteKit website. In this example we'll be including URLs for the homepage, the contact page, the blog, all the blog posts, and all the tag pages.

We're going to be getting pages from a Prismic repository but the same process can be done using any API that returns pages or you could just add static pages if you don't have any dynamically created pages.

Create the sitemap

Creating the sitemap is pretty simple. In your routes directory create a sitemap.xml folder with a +server.js file inside. Then you just need to export a GET handler that returns the xml.


import createClient from '$lib/prismicio';

// set the website url to a variable we can reference in the sitemap
const website = '';

export async function GET({ fetch, request }) {
  const client = createClient({ fetch, request });
   * Get all the blog posts created in Prismic: /blog/[uid]
  const posts = await client.getByType('post');

   * - Get all the tags created in Prismic
  const allTags = await client.getTags();

  return new Response(
    <?xml version="1.0" encoding="UTF-8" ?>
      ${posts ? => `<url>
      </url>`).join('') : ''}
      ${allTags ? => {
        return `<url>
      }).join('') : ''}
      headers: {
        'Content-Type': 'application/xml'