Set canonicals and sitemap with Astro integrations
Astro needs a production site value before canonicals and sitemaps can be correct. Set site in astro.config.mjs, install the @astrojs/sitemap integration, and let it emit sitemap-index.xml with child files. Then render an absolute canonical in the layout from Astro.url and Astro.site so every page references itself.
Why this matters
One config value makes URL and sitemap generation trustworthy.
Before you start
- An Astro project with a production domain
- Ability to run a production build locally or in CI
How to apply it
Set the production site
In astro.config.mjs set site: 'your production domain' using the production origin with no trailing slash. This value feeds canonicals, RSS links and sitemap entries.
Check: grep -n 'site:' astro.config.mjs shows the production origin and astro build finishes without a site warning.
Install the sitemap integration
Run npx astro add sitemap, which installs @astrojs/sitemap and adds it to integrations. Build and confirm sitemap-index.xml and its children land in dist/.
Check: dist/sitemap-index.xml exists after astro build and contains only absolute https URLs.
Render a canonical in the layout
In src/layouts/Base.astro compute const canonical = new URL(Astro.url.pathname, Astro.site) and add link rel=canonical href={canonical} in the head so each route declares itself.
Check: curl -s your production domain/ | grep -c 'rel="canonical"' returns 1 with an absolute href.
Filter drafts and previews
Use the filter option of the sitemap integration to drop draft, preview and thin archive routes, while keeping paginated pages that are meant to be indexable.
Check: The built sitemap contains no preview or draft URLs and every loc returns 200.
Deploy and submit
Deploy the build, request sitemap-index.xml in production and add it in Search Console. Confirm robots.txt links to the same index rather than an old sitemap file.
Check: curl -s your production domain/sitemap-index.xml returns 200 and Search Console reports the sitemap as Success.
Source: seo-mcp playbook · curated entry · report an error
Framework notes
astro
Sitemap entries come from `site` in `astro.config.mjs`; render canonicals in `src/layouts/Base.astro` from `new URL(Astro.url.pathname, Astro.site)`.
static-html
For a plain static build, write the same absolute canonical into each file and generate sitemap.xml at build time from the page list.