@astrojs/ sitemap
このコンテンツはまだ日本語訳がありません。
This Astro integration generates a sitemap based on your pages when you build your Astro project.
Why Astro Sitemap
Section titled Why Astro SitemapA 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 to learn more.
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.
With Astro Sitemap, you don’t have to worry about creating this XML file yourself: the Astro Sitemap integration will crawl your statically-generated routes and create the sitemap file, including dynamic routes like [...slug]
or src/pages/[lang]/[version]/info.astro
generated by getStaticPaths()
.
This integration cannot generate sitemap entries for dynamic routes in SSR mode.
Installation
Section titled InstallationAstro includes an astro add
command to automate the setup of official integrations. If you prefer, you can install integrations manually instead.
Run one of the following commands in a new terminal window.
If you run into any issues, feel free to report them to us on GitHub and try the manual installation steps below.
Manual Install
Section titled Manual InstallFirst, install the @astrojs/sitemap
package using your package manager.
Then, apply the integration to your astro.config.*
file using the integrations
property:
Usage
Section titled Usage@astrojs/sitemap
needs to know your site’s deployed URL to generate a sitemap.
Add your site’s URL as the site
option in astro.config.mjs
. This must begin with http://
or https://
.
With the sitemap integration configured, sitemap-index.xml
and sitemap-0.xml
files will be added to your output directory when building your site.
sitemap-index.xml
links to all the numbered sitemap files.
sitemap-0.xml
lists the pages on your site.
For extremely large sites, there may also be additional numbered files like sitemap-1.xml
and sitemap-2.xml
.
Example of generated files for a two-page website
Sitemap discovery
Section titled Sitemap discoveryYou can make it easier for crawlers to find your sitemap with links in your site’s <head>
and robots.txt
file.
Sitemap link in <head>
Section titled Sitemap link in <head>Add a <link rel="sitemap">
element to your site’s <head>
pointing to the sitemap index file:
Sitemap link in robots.txt
Section titled Sitemap link in robots.txtIf you have a robots.txt
for your website, you can add the URL for the sitemap index to help crawlers:
If you want to reuse the site
value from astro.config.mjs
, you can also generate robots.txt
dynamically.
Instead of using a static file in the public/
directory, create a src/pages/robots.txt.ts
file and add the following code:
Configuration
Section titled ConfigurationTo configure this integration, pass an object to the sitemap()
function in astro.config.mjs
.
filter
Section titled filterAll pages are included in your sitemap by default. By adding a custom filter
function, you can filter included pages by URL.
The function will be called for every page on your site. The page
function parameter is the full URL of the page currently under consideration, including your site
domain. Return true
to include the page in your sitemap, and false
to leave it out.
To filter multiple pages, add arguments with target URLs.
customPages
Section titled customPagesIn 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.
entryLimit
Section titled entryLimitThe 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.
changefreq, lastmod, and priority
Section titled changefreq, lastmod, and priorityThese options correspond to the <changefreq>
, <lastmod>
, and <priority>
tags in the Sitemap XML specification.
Note that changefreq
and priority
are ignored by Google.
Due to limitations of Astro’s Integration API, 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.
serialize
Section titled serializeA function called for each sitemap entry just before writing to a disk. This function can be asynchronous.
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 onSitemapItem
.changefreq
lastmod
(ISO formatted date,String
type)priority
links
.
This links
property contains a LinkItem
list of alternate pages including a parent page.
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).
The serialize
function should return SitemapItem
, touched or not.
The example below shows the ability to add sitemap specific properties individually.
To localize a sitemap, pass an object to this i18n
option.
This object has two required properties:
defaultLocale
:String
. Its value must exist as one oflocales
keys.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.
Read more about language attributes.
The resulting sitemap looks like this:
xslURL
Section titled xslURLThe URL of an XSL stylesheet to style and prettify your sitemap.
The value set can be either a path relative to your configured site
URL for a local stylesheet, or can be an absolute URL link to an external stylesheet.
Examples
Section titled Examples- The official Astro website uses Astro Sitemap to generate its sitemap.
- Browse projects with Astro Sitemap on GitHub for more examples!