Skip to content

Discoverability · SvelteKit

How to add a sitemap.xml in SvelteKit

A sitemap is how you tell a search engine which pages exist and when they last changed, rather than waiting for every one of them to be found by following links. Without it, pages that are not linked prominently can go months before they are crawled at all.

The fix for SvelteKit

13x detects your framework from the response and hands you this version rather than the generic one — below 50% confidence it hedges and gives you the generic one instead.

Add src/routes/sitemap.xml/+server.ts:

ts code
export const prerender = true;

export async function GET() {
  const pages = ["", "pricing", "blog"];
  const body = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${pages.map((page) => `  <url><loc>https://example.com/${page}</loc></url>`).join("\n")}
</urlset>`;

  return new Response(body, { headers: { "content-type": "application/xml" } });
}

On a different stack? The general version of this fix explains what 13x checks and why it matters, without assuming a framework.

Check your SvelteKit site

113 deterministic checks against your live URL, in about 30 seconds. Framework detected from the response, so every fix comes back in the form your stack actually uses.

No signup. Results in 30 seconds.

The same fix, other stacks