Sitemap in 43 lines of Jinja #3542
seanh
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Wanted to share the approach I'm using to generate my
sitemap.xmlfile. I'm aware of the pelican-sitemap plugin, but my solution is <50 lines of template code and it supports articles, pages, the index page, the archives page, the tags, categories and authors pages, tag, category and author index pages, and period archives.I added this to my
pelicanconf.py:Then added this
templates/sitemap.htmlfile:Notes:
The filename extension needs to be
templates/sitemap.htmleven though it will actually be rendered as/sitemap.xml(according to theSITEMAP_SAVE_ASsetting): Pelican seems to expect everything inDIRECT_TEMPLATESto be.html, I couldn't get it to accept atemplates/sitemap.xmlfile.I omitted
<changefreq>and<priority>from the sitemap. These are optional in the sitemap spec and Google ignores them anyway, and I didn't have sensible values to set them to. These could be easily added but I think it's better to omit them.For articles the
<lastmod>will be the article's modified time if it has one, otherwise its created time.Pages don't have a created time so
<lastmod>will be the page's modified time if it has one or will be omitted (<lastmod>is optional in the sitemap spec).The
<lastmod>of the index and archives pages is the created time of the most recent article on the site. (Modified times are deliberately ignored here.)The
<lastmod>of tag, category, author and period archive pages is the created time of the latest article on that page.For the tags, categories and authors pages it first finds the oldest article for each tag/category/author and then takes the newest of these oldest articles, and uses that as the
<lastmod>for the page. This is an attempt to set the tag/category/author page's<lastmod>to the last time that a new tag/category/author was created.If your site disables the archives, tags, categories, authors, tag index, category index, author index or period archives pages they'll be omitted from the sitemap.
Pagination pages beyond the first page are omitted from the sitemap, which I think is fine.
Beta Was this translation helpful? Give feedback.
All reactions