Astro Sitemap: @astrojs/sitemap Integration Guide
Astro has an official sitemap integration maintained by the Astro team: @astrojs/sitemap. It hooks into the build process, discovers all your static pages automatically, and generates sitemap-index.xml plus individual sitemap files at build time. Because Astro defaults to static generation, the sitemap is a simple static file that loads instantly.
Installation
The fastest way: npx astro add sitemap. This installs the package and updates your astro.config.mjs automatically. Or manually: npm install @astrojs/sitemap, then update your config:
The site option is required - without it the integration cannot build absolute URLs and will throw an error. Run npm run build and your sitemap will appear in the dist/ folder as sitemap-index.xml.
Excluding Pages
The filter function receives the full URL of each page and returns true to include it, false to exclude it. Use this to keep admin pages, API routes, and low-value pages out of your sitemap.
Dynamic Routes and Content Collections
Static pages and pages generated from Astro content collections using getStaticPaths() are discovered automatically. The integration crawls your build output, so any page that gets built as an HTML file appears in the sitemap. For SSR (server-rendered) pages, the integration cannot discover URLs automatically since they are not built as static files. You need to manually specify these pages using the customPages option.
Common Astro Sitemap Issues
Sitemap missing after build: If sitemap-index.xml does not appear in dist/, check that the integration is correctly listed in the integrations array and that site is set. Run the build with verbose logging: npx astro build --verbose to see if the integration runs.
Wrong URLs in sitemap: If URLs use localhost or a wrong domain, your site config is missing or incorrect. Ensure site in astro.config.mjs matches your exact production domain including https:// and any subdomain. Use environment variables: site: import.meta.env.SITE_URL or process.env.SITE_URL.