Sitemap
Guidelines for generating a sitemap using the @astrojs/sitemap integration.
astro
## Astro Sitemap Integration Guidelines
1. Purpose: Automatically generates a `sitemap.xml` file for your Astro project during the build process. Sitemaps help search engines discover and crawl your site's pages more efficiently.
2. Installation: Install using the Astro CLI:
```bash
npx astro add sitemap
# or pnpm add @astrojs/sitemap / yarn add @astrojs/sitemap
```
This will install the package and add it to your `astro.config.mjs`.
3. Prerequisite: You must have the `site` property configured in your `astro.config.mjs` with your website's final production URL.
```javascript
// astro.config.mjs
import { defineConfig } from "astro/config";
import sitemap from "@astrojs/sitemap";
export default defineConfig({
site: "https://www.my-awesome-site.dev", // Required!
integrations: [sitemap()],
});
```
4. Basic Usage: Once installed and configured with `site`, the integration automatically crawls your statically generated pages and creates `sitemap.xml` (and `sitemap-index.xml` if needed) in the output directory (usually `dist/`) during `astro build`.
5. Limitations:
- Does not automatically include routes generated on-demand (SSR routes using `export const prerender = false` or `output: 'server'`). These must be added manually using the `customPages` option if needed.
6. Configuration Options (Passed to `sitemap()` in `astro.config.mjs`):
- `filter`: `(pageUrl: string) => boolean` - Function to exclude specific pages based on their URL. Return `false` to exclude.
- `customPages`: `string[]` - Array of absolute URLs to manually include pages not automatically detected by Astro (e.g., SSR pages, externally hosted pages relevant to the site).
- `entryLimit`: `number` (default: 45000) - Maximum number of URLs per sitemap file. If exceeded, creates a sitemap index and multiple sitemap files.
- `changefreq`: `string` - Sets the default `<changefreq>` value for all pages (e.g., `'daily'`, `'weekly'`). Note: Often ignored by search engines.
- `lastmod`: `Date` - Sets a default `<lastmod>` date for all pages.
- `priority`: `number` - Sets a default `<priority>` value (0.0 to 1.0) for all pages. Note: Often ignored by search engines.
- `serialize`: `(item: SitemapItem) => SitemapItem | undefined` - Advanced: Function called for _each_ sitemap entry before writing. Allows modifying properties (`url`, `changefreq`, `lastmod`, `priority`, `links`) or excluding the item (return `undefined`). Useful for setting per-page metadata.
- `i18n`: `{ defaultLocale: string, locales: Record<string, string> }` - Configures internationalization support, generating `<xhtml:link rel="alternate" ...>` tags for localized versions of pages based on URL structure.
- `xslURL`: `string` - URL (absolute or relative to `site`) of an XSL stylesheet to format the sitemap for human viewing in browsers.
7. Sitemap Discovery: While not part of the integration itself, it's recommended to add a link to your sitemap in your `robots.txt` file:
```
Sitemap: https://www.my-awesome-site.dev/sitemap-index.xml
```
(Or `sitemap.xml` if you don't exceed the `entryLimit`).
Reference: [@astrojs/sitemap Integration Docs](mdc:https:/docs.astro.build/en/guides/integrations-guide/sitemap)