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.

mediumIn codeimpact effort

Why this matters

One config value makes URL and sitemap generation trustworthy.

Get the full breakdown

The extended brief explains when this works, when it does not, and the trade-offs. Join the waitlist and it unlocks for every tip when sign-in opens; MCP access is the paid part.

Join the waitlist

Before you start

  • An Astro project with a production domain
  • Ability to run a production build locally or in CI

How to apply it

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

Get the prompt for your agent

A ready-to-paste prompt for your agent that implements this tip and verifies the result in your repo.

Join the waitlist

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.

Official references

Let your agent apply this + 161 more tips.

Pre-order — $19