2022-03-25 16:45:34 -05:00
# @astrojs/sitemap 🗺
2022-06-30 11:02:39 -05:00
This ** [Astro integration][astro-integration]** generates a sitemap based on your routes when you build your Astro project.
2022-03-25 16:45:34 -05:00
2022-06-30 11:02:39 -05:00
- < strong > [Why Astro Sitemap](#why-astro-sitemap)</ strong >
- < strong > [Installation](#installation)</ strong >
- < strong > [Usage](#usage)</ strong >
- < strong > [Configuration](#configuration)</ strong >
- < strong > [Examples](#examples)</ strong >
- < strong > [Troubleshooting](#troubleshooting)</ strong >
- < strong > [Contributing](#contributing)</ strong >
- < strong > [Changelog](#changelog)</ strong >
2022-03-25 16:45:34 -05:00
2022-06-30 11:02:39 -05:00
## Why Astro Sitemap
2022-03-25 16:45:34 -05:00
2022-06-30 11:02:39 -05:00
A Sitemap is an XML file that outlines all of the pages, videos, and files on your site. Search engines like Google read this file to crawl your site more efficiently. [See Google's own advice on sitemaps ](https://developers.google.com/search/docs/advanced/sitemaps/overview ) to learn more.
2022-03-25 16:45:34 -05:00
2022-06-30 11:02:39 -05:00
A sitemap file is recommended for large multi-page sites. If you don't use a sitemap, most search engines will still be able to list your site's pages, but a sitemap is a great way to ensure that your site is as search engine friendly as possible.
2022-03-25 16:45:34 -05:00
2022-06-30 11:02:39 -05:00
With Astro Sitemap, you don't have to worry about creating this file: build your Astro site how you normally would, and the Astro Sitemap integration will crawl your routes and create the sitemap file.
2022-03-25 16:45:34 -05:00
2022-06-30 11:02:39 -05:00
## Installation
2022-03-25 16:45:34 -05:00
2022-06-30 11:02:39 -05:00
< details >
< summary > Quick Install< / summary >
2022-07-21 12:33:59 -05:00
The `astro add` command-line tool automates the installation for you. Run one of the following commands in a new terminal window. (If you aren't sure which package manager you're using, run the first command.) Then, follow the prompts, and type "y" in the terminal (meaning "yes") for each one.
2022-06-30 11:02:39 -05:00
```sh
# Using NPM
npx astro add sitemap
# Using Yarn
yarn astro add sitemap
# Using PNPM
pnpx astro add sitemap
```
Then, restart the dev server by typing `CTRL-C` and then `npm run astro dev` in the terminal window that was running Astro.
Because this command is new, it might not properly set things up. If that happens, [feel free to log an issue on our GitHub ](https://github.com/withastro/astro/issues ) and try the manual installation steps below.
< / details >
< details >
< summary > Manual Install< / summary >
First, install the `@astrojs/sitemap` package using your package manager. If you're using npm or aren't sure, run this in the terminal:
```sh
2022-03-25 16:45:34 -05:00
npm install @astrojs/sitemap
```
Then, apply this integration to your `astro.config.*` file using the `integrations` property:
2022-07-11 14:10:34 -05:00
__`astro.config.mjs`__
2022-03-25 16:45:34 -05:00
```js
2022-06-30 11:02:39 -05:00
import { defineConfig } from 'astro/config';
2022-03-25 16:45:34 -05:00
import sitemap from '@astrojs/sitemap';
2022-06-30 11:02:39 -05:00
export default defineConfig({
2022-03-25 16:45:34 -05:00
// ...
integrations: [sitemap()],
2022-06-30 11:02:39 -05:00
})
2022-03-25 16:45:34 -05:00
```
2022-06-30 11:02:39 -05:00
Then, restart the dev server.
< / details >
2022-03-25 16:45:34 -05:00
2022-06-30 11:02:39 -05:00
## Usage
2022-03-25 16:45:34 -05:00
2022-06-30 11:02:39 -05:00
`@astrojs/sitemap` requires a deployment / site URL for generation. Add your site's URL under your `astro.config.*` using the `site` property. This must begin with `http:` or `https:` .
2022-03-25 16:45:34 -05:00
2022-07-11 14:10:34 -05:00
__`astro.config.mjs`__
2022-03-25 16:45:34 -05:00
```js
2022-06-30 11:02:39 -05:00
import { defineConfig } from 'astro/config';
2022-03-25 16:45:34 -05:00
import sitemap from '@astrojs/sitemap';
2022-06-30 11:02:39 -05:00
export default defineConfig({
2022-03-25 16:45:34 -05:00
// ...
2022-04-02 13:29:59 -05:00
site: 'https://stargazers.club',
2022-03-25 16:45:34 -05:00
integrations: [sitemap()],
2022-06-30 11:02:39 -05:00
})
2022-03-25 16:45:34 -05:00
```
2022-06-30 11:02:39 -05:00
Note that unlike other configuration options, `site` is set in the root `defineConfig` object, rather than inside the `sitemap()` call.
2022-07-19 14:02:30 -05:00
Now, [build your site for production ](https://docs.astro.build/en/reference/cli-reference/#astro-build ) via the `astro build` command. You should find your sitemap under `dist/` for both `sitemap-index.xml` and `sitemap-0.xml` !
2022-06-16 14:06:48 -05:00
2022-06-30 11:02:39 -05:00
> **Warning**
2022-07-19 14:02:30 -05:00
> If you forget to add a `site`, you'll get a friendly warning when you build, and the `sitemap-index.xml` file won't be generated.
2022-06-30 11:02:39 -05:00
< details >
2022-07-11 14:10:34 -05:00
< summary > Example of generated files for a two-page website< / summary >
2022-06-16 14:06:48 -05:00
2022-07-11 14:10:34 -05:00
**`sitemap-index.xml`**
2022-06-16 14:06:48 -05:00
```xml
<?xml version="1.0" encoding="UTF-8"?>
< sitemapindex xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9" >
< sitemap >
< loc > https://stargazers.club/sitemap-0.xml< / loc >
< / sitemap >
< / sitemapindex >
```
2022-07-11 14:10:34 -05:00
**`sitemap-0.xml`**
2022-06-16 14:06:48 -05:00
```xml
<?xml version="1.0" encoding="UTF-8"?>
< urlset xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news = "http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml = "http://www.w3.org/1999/xhtml" xmlns:image = "http://www.google.com/schemas/sitemap-image/1.1" xmlns:video = "http://www.google.com/schemas/sitemap-video/1.1" >
< url >
< loc > https://stargazers.club/< / loc >
< / url >
< url >
< loc > https://stargazers.club/second-page/< / loc >
< / url >
< / urlset >
```
2022-06-30 11:02:39 -05:00
< / details >
2022-03-25 16:45:34 -05:00
2022-04-02 13:29:59 -05:00
2022-06-30 11:02:39 -05:00
## Configuration
2022-04-02 13:29:59 -05:00
2022-06-30 11:02:39 -05:00
To configure this integration, pass an object to the `sitemap()` function call in `astro.config.mjs` .
2022-04-02 13:29:59 -05:00
2022-07-11 14:10:34 -05:00
__`astro.config.mjs`__
2022-04-02 13:29:59 -05:00
```js
2022-06-30 11:02:39 -05:00
...
export default defineConfig({
integrations: [sitemap({
filter: ...
})]
});
2022-04-02 13:29:59 -05:00
```
2022-06-30 11:02:39 -05:00
< details >
< summary > < strong > filter< / strong > < / summary >
All pages are included in your sitemap by default. By adding a custom `filter` function, you can filter included pages by URL.
2022-06-23 10:31:54 -05:00
2022-07-11 14:10:34 -05:00
__`astro.config.mjs`__
2022-06-23 10:31:54 -05:00
```js
2022-06-30 11:02:39 -05:00
...
2022-06-23 10:31:54 -05:00
sitemap({
2022-06-30 11:02:39 -05:00
filter: (page) => page !== 'https://stargazers.club/secret-vip-lounge'
2022-06-23 10:31:54 -05:00
}),
```
2022-06-30 11:02:39 -05:00
The function will be called for every page on your site. The `page` function parameter is the full URL of the page currently under considering, including your `site` domain. Return `true` to include the page in your sitemap, and `false` to leave it out.
< / details >
2022-04-02 13:29:59 -05:00
2022-06-30 11:02:39 -05:00
< details >
< summary > < strong > customPages< / strong > < / summary >
In some cases, a page might be part of your deployed site but not part of your Astro project.
If you'd like to include a page in your sitemap that _isn't_ created by Astro, you can use this option.
2022-04-02 13:29:59 -05:00
2022-07-11 14:10:34 -05:00
__`astro.config.mjs`__
2022-04-02 13:29:59 -05:00
```js
2022-06-30 11:02:39 -05:00
...
2022-04-02 13:29:59 -05:00
sitemap({
2022-06-30 11:02:39 -05:00
customPages: ['https://stargazers.club/external-page', 'https://stargazers.club/external-page2']
2022-04-02 13:29:59 -05:00
}),
```
2022-06-30 11:02:39 -05:00
< / details >
2022-04-02 13:29:59 -05:00
2022-06-30 11:02:39 -05:00
< details >
2022-07-11 14:10:34 -05:00
< summary > < strong > entryLimit< / strong > < / summary >
2022-06-16 14:06:48 -05:00
2022-06-30 11:02:39 -05:00
The maximum number entries per sitemap file. The default value is 45000. A sitemap index and multiple sitemaps are created if you have more entries. See this [explanation of splitting up a large sitemap ](https://developers.google.com/search/docs/advanced/sitemaps/large-sitemaps ).
2022-06-16 14:06:48 -05:00
2022-07-11 14:10:34 -05:00
__`astro.config.mjs`__
2022-06-16 14:06:48 -05:00
```js
import sitemap from '@astrojs/sitemap';
export default {
site: 'https://stargazers.club',
integrations: [
sitemap({
entryLimit: 10000,
}),
],
}
```
2022-06-30 11:02:39 -05:00
< / details >
2022-06-16 14:06:48 -05:00
2022-06-30 11:02:39 -05:00
< details >
2022-07-11 14:10:34 -05:00
< summary > < strong > changefreq< / strong > , < strong > lastmod< / strong > , and < strong > priority< / strong > < / summary >
2022-06-16 14:06:48 -05:00
2022-06-30 11:02:39 -05:00
These options correspond to the `<changefreq>` , `<lastmod>` , and `<priortity>` tags in the [Sitemap XML specification. ](https://www.sitemaps.org/protocol.html )
2022-06-16 14:06:48 -05:00
2022-06-30 11:02:39 -05:00
Note that `changefreq` and `priority` are ignored by Google.
2022-06-16 14:06:48 -05:00
2022-06-30 11:02:39 -05:00
> **Note**
> Due to limitations of Astro's [Integration API](https://docs.astro.build/en/reference/integrations-reference/), this integration can't analyze a given page's source code. This configuration option can set `changefreq`, `lastmod` and `priority` on a _site-wide_ basis; see the next option **serialize** for how you can set these values on a per-page basis.
2022-06-16 14:06:48 -05:00
2022-07-11 14:10:34 -05:00
__`astro.config.mjs`__
2022-06-16 14:06:48 -05:00
```js
import sitemap from '@astrojs/sitemap';
export default {
site: 'https://stargazers.club',
integrations: [
sitemap({
changefreq: 'weekly',
priority: 0.7,
lastmod: new Date('2022-02-24'),
}),
],
}
```
2022-06-30 11:02:39 -05:00
< / details >
2022-06-16 14:06:48 -05:00
2022-06-30 11:02:39 -05:00
< details >
< summary >
< strong > serialize< / strong >
< / summary >
2022-06-16 14:06:48 -05:00
2022-06-30 11:02:39 -05:00
A function called for each sitemap entry just before writing to a disk. This function can be asynchronous.
2022-06-16 14:06:48 -05:00
2022-06-30 11:02:39 -05:00
It receives as its parameter a `SitemapItem` object that can have these properties:
- `url` (absolute page URL). This is the only property that is guaranteed to be on `SitemapItem` .
- `changefreq`
- `lastmod` (ISO formatted date, `String` type)
- `priority`
- `links` .
2022-06-16 14:06:48 -05:00
2022-06-30 11:02:39 -05:00
This `links` property contains a `LinkItem` list of alternate pages including a parent page.
2022-06-16 14:06:48 -05:00
2022-06-30 11:02:39 -05:00
The `LinkItem` type has two fields: `url` (the fully-qualified URL for the version of this page for the specified language) and `lang` (a supported language code targeted by this version of the page).
2022-06-27 13:02:00 -05:00
2022-06-30 11:02:39 -05:00
The `serialize` function should return `SitemapItem` , touched or not.
The example below shows the ability to add sitemap specific properties individually.
2022-06-16 14:06:48 -05:00
2022-07-11 14:10:34 -05:00
__`astro.config.mjs`__
2022-06-16 14:06:48 -05:00
```js
import sitemap from '@astrojs/sitemap';
export default {
site: 'https://stargazers.club',
integrations: [
sitemap({
serialize(item) {
2022-06-27 13:02:00 -05:00
if (/exclude-from-sitemap/.test(item.url)) {
return undefined;
}
2022-06-16 14:06:48 -05:00
if (/your-special-page/.test(item.url)) {
item.changefreq = 'daily';
item.lastmod = new Date();
item.priority = 0.9;
}
return item;
},
}),
],
}
```
2022-06-30 11:02:39 -05:00
< / details >
< details >
< summary >
< strong > i18n< / strong >
< / summary >
2022-06-16 14:06:48 -05:00
2022-06-30 11:02:39 -05:00
To localize a sitemap, pass an object to this `i18n` option.
This object has two required properties:
2022-06-16 14:06:48 -05:00
- `defaultLocale` : `String` . Its value must exist as one of `locales` keys.
2022-06-30 11:02:39 -05:00
- `locales` : `Record<String, String>` , key/value - pairs. The key is used to look for a locale part in a page path. The value is a language attribute, only English alphabet and hyphen allowed.
2022-06-16 14:06:48 -05:00
2022-06-30 11:02:39 -05:00
[Read more about language attributes ](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang ).
2022-06-16 14:06:48 -05:00
2022-06-30 11:02:39 -05:00
[Read more about localization ](https://developers.google.com/search/docs/advanced/crawling/localized-versions#all-method-guidelines ).
2022-06-16 14:06:48 -05:00
2022-07-11 14:10:34 -05:00
__`astro.config.mjs`__
2022-06-16 14:06:48 -05:00
```js
import sitemap from '@astrojs/sitemap';
export default {
site: 'https://stargazers.club',
integrations: [
sitemap({
i18n: {
defaultLocale: 'en', // All urls that don't contain `es` or `fr` after `https://stargazers.club/` will be treated as default locale, i.e. `en`
locales: {
en: 'en-US', // The `defaultLocale` value must present in `locales` keys
es: 'es-ES',
fr: 'fr-CA',
},
},
}),
],
};
```
2022-06-30 11:02:39 -05:00
< details >
< summary > The resulting sitemap looks like this< / summary >
2022-06-16 14:06:48 -05:00
```xml
...
< url >
< loc > https://stargazers.club/< / loc >
< xhtml:link rel = "alternate" hreflang = "en-US" href = "https://stargazers.club/" / >
< xhtml:link rel = "alternate" hreflang = "es-ES" href = "https://stargazers.club/es/" / >
< xhtml:link rel = "alternate" hreflang = "fr-CA" href = "https://stargazers.club/fr/" / >
< / url >
< url >
< loc > https://stargazers.club/es/< / loc >
< xhtml:link rel = "alternate" hreflang = "en-US" href = "https://stargazers.club/" / >
< xhtml:link rel = "alternate" hreflang = "es-ES" href = "https://stargazers.club/es/" / >
< xhtml:link rel = "alternate" hreflang = "fr-CA" href = "https://stargazers.club/fr/" / >
< / url >
< url >
< loc > https://stargazers.club/fr/< / loc >
< xhtml:link rel = "alternate" hreflang = "en-US" href = "https://stargazers.club/" / >
< xhtml:link rel = "alternate" hreflang = "es-ES" href = "https://stargazers.club/es/" / >
< xhtml:link rel = "alternate" hreflang = "fr-CA" href = "https://stargazers.club/fr/" / >
< / url >
< url >
< loc > https://stargazers.club/es/second-page/< / loc >
< xhtml:link rel = "alternate" hreflang = "es-ES" href = "https://stargazers.club/es/second-page/" / >
< xhtml:link rel = "alternate" hreflang = "fr-CA" href = "https://stargazers.club/fr/second-page/" / >
< xhtml:link rel = "alternate" hreflang = "en-US" href = "https://stargazers.club/second-page/" / >
< / url >
...
```
2022-06-30 11:02:39 -05:00
< / details >
< / details >
## Examples
2022-07-19 14:02:30 -05:00
- The official Astro website uses Astro Sitemap to generate [its sitemap ](https://astro.build/sitemap-index.xml ).
2022-06-30 11:02:39 -05:00
- The [integrations playground template ](https://github.com/withastro/astro/tree/latest/examples/integrations-playground?on=github ) comes with Astro Sitemap installed. Try adding a route and building the project!
- [Browse projects with Astro Sitemap on GitHub ](https://github.com/search?q=%22@astrojs/sitemap%22+filename:package.json&type=Code ) for more examples!
## Troubleshooting
2022-07-11 14:10:34 -05:00
For help, check out the `#support-threads` channel on [Discord ](https://astro.build/chat ). Our friendly Support Squad members are here to help!
You can also check our [Astro Integration Documentation][astro-integration] for more on integrations.
2022-06-30 11:02:39 -05:00
## Contributing
This package is maintained by Astro's Core team. You're welcome to submit an issue or PR!
## Changelog
2022-07-11 14:10:34 -05:00
See [CHANGELOG.md ](CHANGELOG.md ) for a history of changes to this integration.
2022-06-30 11:02:39 -05:00
2022-03-25 16:45:34 -05:00
[astro-integration]: https://docs.astro.build/en/guides/integrations-guide/