Free Astro tutorials for complete beginners.

Beginner's Guide to Astro

How to Add a Sitemap to Astro

Create 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 that search engines can read. Astro has an official integration that writes one every time you build.

Step 1: Add the integration

Run this in your project folder and confirm the changes:

npx astro add sitemap

Step 2: Set your site’s address

The sitemap needs your site’s full address. Open astro.config.mjs and 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, including https://.

Step 3: Build and check

Run npm run build. Two new files appear in dist:

  • sitemap-index.xml, which points to the other file
  • sitemap-0.xml, which lists your pages

Open sitemap-0.xml and you’ll see one entry for each page, like this:

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

Tell search engines where it is

Add a line to public/robots.txt. Create the file if it doesn’t exist:

User-agent: *
Allow: /

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

It also helps to add a link in your layout’s <head>:

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

Finally, submit the sitemap address in Google Search Console once your site is live.

Leaving pages out

Some pages shouldn’t be in a sitemap, like a search page or a thank-you page. Use the filter option:

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

Sitemaps are 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 discover pages, especially 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 in the official Astro Discord, where the support channels are friendly to beginners.

Keep reading

  1. How to Deploy an Astro Site to Cloudflare Workers

    Put your Astro site online with Cloudflare Workers in four steps. This is how AstroBeginner itself is hosted, so every step has been run for real.

    Tutorials, 7 minute read, November 6, 2026

  2. How to Add Tailwind CSS to Astro

    Add Tailwind CSS to an Astro site with one command, then style your first element. Tested on Astro 7.4 and Tailwind 4.

    Tutorials, 5 minute read, November 6, 2026

  3. How to Add Images to an Astro Site

    Add an image to your Astro site, and let Astro shrink and convert it automatically. One test image went from 62 kB to 5 kB.

    Tutorials, 6 minute read, November 6, 2026

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