Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
81 lines (68 sloc) 2.37 KB
---
converter: markdown
metadata:
title: Creating a Dynamic Sitemap
description: This guide will help you create a dynamic /sitemap.xml page.
slug: best-practices/seo/creating-dynamic-sitemap
searchable: true
---
This guide will help you create a dynamic `/sitemap.xml` page.
## Requirements
This is an advanced tutorial. To follow it, you should be familiar with basic platformOS concepts, HTML, GraphQL queries, Liquid, topics in the Get Started section and tutorials related to Pages.
* [How platformOS Works](/how-platformos-works)
* [Get Started](/get-started)
* [Pages tutorials](/tutorials/pages/pages)
## Steps
Creating a dynamic sitemap.xml is a two-step process:
<div data-autosteps></div>
### Step 1: Prepare GraphQL query
Create a GraphQL query which fetches all pages except for the ones that are explicitly excluded via the proper metadata key:
```graphql
query sitemap_pages($per_page: Int = 1000, $page: Int = 1) {
pages: admin_pages(
per_page: $per_page,
page: $page,
filter: {
metadata: {
has_key: "skip_sitemap"
exclude: true
}
}
) {
total_entries
total_pages
results {
slug
}
}
}
```
### Step 2: Setup page
Create `views/pages/sitemap.xml.liquid` page. Use the GraphQL query defined above to iterate dynamically over all pages.
```yaml
---
slug: sitemap
layout_name: ""
static_cache:
expire: 3600
metadata:
skip_sitemap: true
---
```
```liquid
{% raw %}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{%- graphql g = 'sitemap_pages' -%}
{% for page in g.pages.results %}
{% if page.slug == '/' %}{% assign slug = page.slug %}{% else %}{% assign slug = '/' | append: page.slug %}{% endif %}
<url>
<loc>https://{{ context.location.host }}{{ slug }}</loc>
<changefreq>{{ page.metadata.sitemap.changefreq | default: 'monthly' }}</changefreq>
<priority>{{ page.metadata.sitemap.priority | default: 1 }}</priority>
</url>
{% endfor %}
</urlset>
{% endraw %}
```
Using this template, you can explicitly exclude any page from the sitemap.xml by including the `skip_sitemap` key to a `metatag` property of the page. You can also use `metatag` to configure any sitemap properties like changefreq and priority. Please note, that the example above already uses static caching for 1 hour - you might want to remove it for development.
You can’t perform that action at this time.