Sitemap
Generate a sitemap automatically for your site
Options See on deno.land
- filename string
The sitemap file name
Default:"/sitemap.xml"
- query string
The query to search pages included in the sitemap
Default:""
- sort string
The values to sort the sitemap
Default:"url=asc"
- lastmod string function
The key to use for the lastmod field or a custom function
Default:"date"
- changefreq string function
The key to use for the changefreq field or a custom function
- priority string function
The key to use for the priority field or a custom function
Description
This plugin generates a sitemap.xml
file automatically with all your pages, which is useful for SEO. See the Sitemaps XML format specification for more info.
It also creates a robots.txt
file that include a link to the sitemap file, so it's easier to discover for the search engines.
Installation
Import this plugin in your _config.ts
file to use it:
import lume from "lume/mod.ts";
import sitemap from "lume/plugins/sitemap.ts";
const site = lume();
site.use(sitemap(/* Options */));
export default site;
Configuration
Internally, this plugin uses Search to search and return the pages. By default, all pages are included in the sitemap (except the 404 page) and sorted by URL. You can setup a different configuration:
site.use(sitemap({
filename: "my-sitemap.xml", // to change the sitemap filename
query: "indexable=true", // Select only pages with the indexable attribute as true
sort: "date=desc", // To sort by data in ascendent order
}));
To define the URL, it uses the location defined in the config file.
lastmod value
By default, the plugin uses the value of the date
variable for lastmod
. This variable can mean anything, but it is not necessarily the last modification date. If you want to use the last modification time of the page file, you can create a preprocessor like this:
// Create the lastmod variable with the mtime of the file
site.preprocess([".html"], (pages) => {
for (const page of pages) {
const info = page.src.entry?.getInfo();
page.data.lastmod = info?.mtime;
}
});
// Configure the plugin to use the variable
site.use(sitemap({
lastmod: "lastmod",
}));
Note
mtime
is not a reliable value. In some CI environments, it's the present time (the moment where the file is created after cloning the repo, instead of when this file content was modified for the last time). Alternatively, you can use Git Last Updated value:
import { getGitDate } from "lume/core/utils/date.ts";
site.preprocess([".html"], (pages) => {
for (const page of pages) {
const { entry } = page.src;
page.data.lastmod = getGitDate("modified", entry.src);
}
});
However, this requires the CI/CD environment to perform a deep clone, instead of a shallow clone only fetching the last commit. For example, Netlify and DigitalOcean perform a deep clone by default, while Vercel and Render can only perform a shallow clone. Cloudflare Pages, GitHub Pages, and GitLab Pages perform a shallow clone by default, but they can be configured to perform a deep clone instead.
Multilanguage
It's possible generate sitemaps for sites with multiple languages using the multilanguage plugin:
import lume from "lume/mod.ts";
import multilanguage from "lume/plugins/multilanguage.ts";
import sitemap from "lume/plugins/sitemap.ts";
const site = lume();
site.use(multilanguage({
languages: ["en", "gl", "es"],
}));
site.use(sitemap(/* Options */));
export default site;