How to Generate a Sitemap and robots.txt on Static Site Deploy
A sitemap lists your URLs for crawlers, and robots.txt points them at it; both should be produced at build time.
Use the framework sitemap integration (Astro @astrojs/sitemap, Next next-sitemap, Hugo built-in) so sitemap.xml is written into the build output, and ship a robots.txt that references its absolute URL.
Steps
- Enable the sitemap integration with your production
site/siteUrl. - Confirm
sitemap.xmlappears in the build output. - Add a
robots.txtthat links the sitemap absolute URL. - Deploy both as part of the static output.
Build + robots.txt
Terminal
# Next: next-sitemap runs after build (postbuild)
- run: npm run build && npx next-sitemap
# robots.txt (in public/, copied into the build):
# User-agent: *
# Allow: /
# Sitemap: https://example.com/sitemap.xmlGotchas
- The sitemap needs the absolute production URL, so set the site config before building.
- A subdirectory deploy must include the base path in every sitemap URL.
- A
Disallow: /left in robots.txt silently blocks the whole site from indexing.
Related guides
How to Set the Base Path for a Static Site Deployed to a SubdirectoryConfigure the base path for a static site served from a subdirectory (like a project Pages URL) so asset and…
How to Deploy a Docs Site (MkDocs, Sphinx, Docusaurus) From CIBuild and deploy a documentation site to GitHub Pages from CI, whether it is MkDocs, Sphinx, or Docusaurus, u…