<a id="sitemap-generation"></a>

# Sitemap 生成器

本文档介绍如何使用 `wagtail.contrib.sitemaps` 模块为你的 Wagtail 网站创建 XML sitemap，
该模块基于 Django 的
[sitemap 框架](https://docs.djangoproject.com/en/stable/ref/contrib/sitemaps/)。

## 基本配置

首先，你需要在 Django 设置文件里把 `"django.contrib.sitemaps"` 加入 INSTALLED_APPS：

```python
INSTALLED_APPS = [
    ...

    "django.contrib.sitemaps",
]
```

然后，在 `urls.py` 中，你需要加入指向
`wagtail.contrib.sitemaps.views.sitemap` 视图的链接，它负责生成
sitemap。请使用这个视图，而不是 Django 的 `django.contrib.sitemaps.views.sitemap`，
后者并不知道 Wagtail 站点的概念：

```python
from wagtail.contrib.sitemaps.views import sitemap

urlpatterns = [
    ...

    path("sitemap.xml", sitemap),

    ...

    # 确保 'sitemap' 这一行出现在默认 Wagtail 页面服务路由之前
    path("", include(wagtail_urls)),
]
```

现在你应该能访问 `/sitemap.xml` 并看到 sitemap
正常工作。默认情况下，它会列出你站点的所有已发布页面，私有页面除外。

为了帮助搜索引擎和其他爬虫找到该 sitemap，请在
`robots.txt` 中列出它：

```text
Sitemap: https://mysite.com/sitemap.xml
```

<a id="sitemap-caching"></a>

## 缓存

sitemap 在每次请求时生成。要缓存它，请用
Django 的 [`cache_page()`](https://docs.djangoproject.com/en/stable/topics/cache/#django.views.decorators.cache.cache_page) 装饰器包装该视图，做法见
[Django 的 sitemap 文档](https://docs.djangoproject.com/en/stable/ref/contrib/sitemaps/#creating-a-sitemap-index)：

```python
from django.views.decorators.cache import cache_page

from wagtail.contrib.sitemaps.views import sitemap

urlpatterns = [
    ...

    path("sitemap.xml", cache_page(60 * 60)(sitemap)),

    ...

    # 确保 'sitemap' 这一行出现在默认 Wagtail 页面服务路由之前
    path("", include(wagtail_urls)),
]
```

这会把 sitemap 在 Django 的缓存中存放一小时，并设置 `Cache-Control: max-age` 头，让 CDN 和
爬虫也能缓存它。在这段时间内发布或取消发布的页面
不会被反映出来，直到缓存过期，所以请挑一个适合你内容变化频率的超时时间。

当页面被发布时，[前端缓存失效器](frontendcache.html.md#frontend-cache-purging)不会清除
sitemap。如果你自己[清除该 sitemap URL](frontendcache.html.md#frontend-cache-invalidating-urls)，
例如在 [`page_published`](../signals.html.md#signal-page-published) 信号处理器中，请改用 [`cache_control()`](https://docs.djangoproject.com/en/stable/topics/http/decorators/#django.views.decorators.cache.cache_control)，
例如 `cache_control(max_age=60 * 60)(sitemap)`，因为 Django 的缓存
并不会被清除。

## 设置主机名

默认情况下，sitemap 使用 Wagtail 管理后台
`Sites` 区域中定义的主机名。如果你的默认站点叫 `localhost`，那么 sitemap 中的 URL 会像这样：

```xml
<url>
    <loc>http://localhost/about/</loc>
    <lastmod>2015-09-26</lastmod>
</url>
```

为了让搜索引擎正确索引你的站点，你需要设置
一个有效的、可爬取的主机名。如果你把站点的主机名从
`localhost` 改为 `mysite.com`，`sitemap.xml` 就会包含正确的 URL。如果你
把站点的端口改为 `443`，就会使用 `https` 协议：

```xml
<url>
    <loc>https://mysite.com/about/</loc>
    <lastmod>2015-09-26</lastmod>
</url>
```

了解更多关于[使用 Sites](../models.html.md#site-model-ref)的内容。

<a id="sitemap-multi-language-support"></a>

## 多语言支持

当 `WAGTAIL_I18N_ENABLED` 为 `True` 时，sitemap 会包含站点所有语言区域的页面树中的页面，包括只存在于某一个语言区域的页面，并且会自动为每个页面的翻译加上替代链接。

```xml
<url>
    <loc>http://mysite.com/en/</loc>
    <lastmod>2025-03-15</lastmod>
    <xhtml:link rel="alternate" hreflang="en" href="http://mysite.com/en/"/>
    <xhtml:link rel="alternate" hreflang="fr" href="http://mysite.com/fr/"/>
</url>
<url>
    <loc>http://mysite.com/fr/</loc>
    <lastmod>2025-03-15</lastmod>
    <xhtml:link rel="alternate" hreflang="en" href="http://mysite.com/en/"/>
    <xhtml:link rel="alternate" hreflang="fr" href="http://mysite.com/fr/"/>
</url>
```

定义 URL 模式时传入 `{"alternates": False}` 可以禁用这一行为：

```python
# /my_project/urls.py

# ...

urlpatterns = [
    ...
    path("sitemap.xml", sitemap, {"alternates": False}),
    ...
]
```

## 定制

### URL

`Page` 类定义了一个 `get_sitemap_urls` 方法，你可以
覆盖它，以按 `Page` 实例定制 sitemap。该方法必须接受
一个 request 对象，并返回一个字典列表，sitemap 中每个 URL
条目对应一个字典。你可以通过返回空列表把页面排除在 sitemap 之外。

每个字典可以包含以下内容：

- **location**（必需） - 要加入 sitemap 的完整 URL 路径。
- **lastmod** - 一个 Python date 或 datetime，设为页面最后修改的时间。
- **changefreq**
- **priority**
- **alternates** - 一个字典列表，包含 `location` 和 `lang_code` 项，表示该页面的翻译。

你可以添加更多内容，但你需要覆盖
`sitemap.xml` 模板，它们才会在 sitemap 中显示。

## 提供多个 sitemap

如果你的站点超过 50,000 个页面，或者要为非 Wagtail 页面（例如你自己的 Django 模型）添加 sitemap，
请使用 sitemap 索引。

要设置它，请按 Django 文档中的 [创建 sitemap 索引](https://docs.djangoproject.com/en/stable/ref/contrib/sitemaps/#creating-a-sitemap-index)
操作，但改用
`wagtail.contrib.sitemaps.views` 中的 `index` 和 `sitemap` 视图，而不是 `django.contrib.sitemaps.views` 中的。
要缓存它们，请按 [缓存](#sitemap-caching)中所述，把两个视图都用 `cache_page` 包装。
