Free Astro tutorials for complete beginners.

Beginner's Guide to Astro

How to Add a Sitemap to Astro

Make a sitemap for your Astro site in three steps, so search engines can find every page. Includes the robots.txt line.

Tested on Astro 7.4

Cover image for How to Add a Sitemap to Astro
In this guide
  1. Step 1: Add the integration
  2. Step 2: Set your site’s address
  3. Step 3: Build and check
  4. Tell search engines where it is
  5. Leaving pages out
  6. Frequently asked questions

A sitemap is a list of your pages. Search engines read it to find them all. Astro has an official integration that writes one each time you build. An integration is an add-on for Astro. A build is making the finished copy of your site.

Step 1: Add the integration

Run this in your project folder. Say yes to the changes it offers:

npx astro add sitemap

Step 2: Set your site’s address

The sitemap needs your site’s full address. Open astro.config.mjs. This is your site’s settings file. Add site:

import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';

export default defineConfig({
  site: 'https://example.com',
  integrations: [sitemap()],
});

Replace https://example.com with your real address. Include the https:// part.

Step 3: Build and check

Run npm run build. Two new files appear in dist. That is the output folder that holds your finished site.

  • sitemap-index.xml. This one points to the other file.
  • sitemap-0.xml. This one lists your pages.

Open sitemap-0.xml. You will see one entry for each page. It looks like this:

<url><loc>https://example.com/about/</loc></url>

Tell search engines where it is

Add a line to public/robots.txt. Make the file if it is not there yet:

User-agent: *
Allow: /

Sitemap: https://example.com/sitemap-index.xml

It also helps to add a link in your layout’s <head>. The layout is the shared frame around every page:

<link rel="sitemap" href="/sitemap-index.xml" />

Last, submit the sitemap address in Google Search Console. Do this once your site is live.

Leaving pages out

Some pages should not be in a sitemap. A search page, say, or a thank-you page. Use the filter option:

sitemap({
  filter: (page) => !page.endsWith('/search/'),
});

A sitemap is one part of being found. For the rest, read our Astro SEO guide.

Frequently asked questions

What is a sitemap?

A sitemap is a file that lists every page on your site. Search engines read it to find pages, above all new ones that nothing links to yet.

Why is my sitemap empty or missing?

The most common cause is a missing site setting in astro.config.mjs. The integration needs your site's full address to build the links.

Stuck on a step?

Check the common errors guide, look up a word in the glossary, or ask the friendly people in the official Astro and EmDash Discords. How to ask so people can help.

Keep reading

  1. How to Register a Domain Name and Connect It to Your Astro Site

    Buy a domain for about $11 a year, then point it at Cloudflare or Netlify. We did this for astronursery.com, and these are the exact steps.

    Tutorials, 8 minute read, November 8, 2026

  2. How to Add Google Analytics to an Astro Site (and a Simpler Option)

    Paste Google's tag into your Astro layout with one small change, is:inline, and it works on every page. Or use Cloudflare Web Analytics, which needs no cookie banner.

    Tutorials, 6 minute read, November 8, 2026

  3. How to Add a Contact Form to an Astro Site (No Server Needed)

    A static site can't receive form submissions on its own. A free form service can. Here's a working contact form in ten lines, with spam protection built in.

    Tutorials, 7 minute read, November 8, 2026

Try "blog", "deploy" or "EmDash".