📦 plugin-sitemap
这个插件会为你的站点创建一个站点地图,以便搜索引擎的爬虫能够更准确地爬取你的网站。
This plugin is always inactive in development and only active in production because it works on the build output.
安装流程
- npm
- Yarn
- pnpm
- Bun
npm install --save @docusaurus/plugin-sitemap
yarn add @docusaurus/plugin-sitemap
pnpm add @docusaurus/plugin-sitemap
bun add @docusaurus/plugin-sitemap
If you use the preset @docusaurus/preset-classic, you don't need to install this plugin as a dependency.
You can configure this plugin through the preset options.
配置
接受的字段:
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
lastmod | 'date' | 'datetime' | null | 'date' | date is YYYY-MM-DD. datetime is a ISO 8601 datetime. null is disabled. See sitemap docs. |
ignorePatterns | string[] | [] | 一列 glob 模式。匹配成功的路径不会出现在站点地图中。注意你可能要在这里加上 base URL。 |
filename | string | sitemap.xml | 创建的站点地图的路径,相对于输出目录。如果你有两个插件实例输出两个文件,会很有用。 |
createSitemapItems | CreateSitemapItemsFn | undefined | undefined | An optional function which can be used to transform and / or filter the items in the sitemap. |
类型
CreateSitemapItemsFn
type CreateSitemapItemsFn = (params: {
siteConfig: DocusaurusConfig;
routes: RouteConfig[];
defaultCreateSitemapItems: CreateSitemapItemsFn;
}) => Promise<SitemapItem[]>;
SitemapItem
type SitemapItem = {
url: string;
lastmod?: string | null;
changefreq?:
| 'always'
| 'hourly'
| 'daily'
| 'weekly'
| 'monthly'
| 'yearly'
| 'never'
| null;
priority?: number | null;
};
See the sitemap protocol docs for the meaning of each field.
这个插件会读取某些站点配置:
noIndex: results in no sitemap generatedtrailingSlash: determines if the URLs in the sitemap have trailing slashes
lastmodThe lastmod option will only output a sitemap \<lastmod> tag if plugins provide route metadata attributes sourceFilePath and/or lastUpdatedAt.
All the official content plugins provide the metadata for routes backed by a content file (Markdown, MDX or React page components), but it is possible third-party plugin authors do not provide this information, and the plugin will not be able to output a \<lastmod> tag for their routes.
示例配置
你可以通过预设选项或插件选项来配置这个插件。
大多数 Docusaurus 用户通过预设选项配置此插件。
- 预设选项
- 插件选项
如果你使用预设,你可以通过预设选项配置这个插件:
module.exports = {
presets: [
[
'@docusaurus/preset-classic',
{
sitemap: {
lastmod: 'date',
ignorePatterns: ['/tags/**'],
filename: 'sitemap.xml',
createSitemapItems: async (params) => {
const {defaultCreateSitemapItems, ...rest} = params;
const items = await defaultCreateSitemapItems(rest);
return items.filter((item) => !item.url.includes('/page/'));
},
},
},
],
],
};
如果你用的是独立插件,直接向插件提供选项:
module.exports = {
plugins: [
[
'@docusaurus/plugin-sitemap',
{
lastmod: 'date',
ignorePatterns: ['/tags/**'],
filename: 'sitemap.xml',
createSitemapItems: async (params) => {
const {defaultCreateSitemapItems, ...rest} = params;
const items = await defaultCreateSitemapItems(rest);
return items.filter((item) => !item.url.includes('/page/'));
},
},
],
],
};
You can find your sitemap at /sitemap.xml.
Emitting changefreq and priority
The plugin doesn't emit \<changefreq> and \<priority> tags, which Google ignores. If you need them, use createSitemapItems to emit distinct values per URL:
- 预设选项
- 插件选项
如果你使用预设,你可以通过预设选项配置这个插件:
module.exports = {
presets: [
[
'@docusaurus/preset-classic',
{
sitemap: {
createSitemapItems: async (params) => {
const {defaultCreateSitemapItems, ...rest} = params;
const items = await defaultCreateSitemapItems(rest);
return items.map((item) => ({
...item,
changefreq: item.url.includes('/blog/') ? 'weekly' : 'monthly',
priority: item.url.includes('/docs/') ? 0.8 : 0.5,
}));
},
},
},
],
],
};
如果你用的是独立插件,直接向插件提供选项:
module.exports = {
plugins: [
[
'@docusaurus/plugin-sitemap',
{
createSitemapItems: async (params) => {
const {defaultCreateSitemapItems, ...rest} = params;
const items = await defaultCreateSitemapItems(rest);
return items.map((item) => ({
...item,
changefreq: item.url.includes('/blog/') ? 'weekly' : 'monthly',
priority: item.url.includes('/docs/') ? 0.8 : 0.5,
}));
},
},
],
],
};