Sorting sitemap.xml pages (?)

It seems to me that it could be a good idea to sort the pages of the sitemap.xml by .Permalink.

It doesn’t change anything for the search engines which read all the pages of the sitemap without taking into account the order.

But there is a big difference if the sitemap is versioned in git. Currently, a sitemap can be different at each compilation, creating another commit while nothing really changes.

If the URLs are sorted by .Permalink, an identical sitemap will not create another commit.

Maybe this is only interesting for me (I also version /public/ for some sites), but I can do a pull request if the approach seems interesting to others.

You can replace the internal sitemap template with your own sitemap template in the layouts directory. There you can sort and add any additional features you like. I don’t think that an optimization of generated output, because the output might be added to a git repository, might not be a feature for the majority.

Check the docs for the initial layout file and then copy to your layout directory and edit. Hugo will then take those instead of the internals.

1 Like

I already have a special sitemap.xml for my website :wink:

:wink: you changed your avatar so I went the “newbie” route without looking at the name :smiley: But the main point stays, not many will add the built site to the repo and it’s probably a special feature, and… sorting takes time away, milliseconds, but that is what we like :wink:

You are right. Sorting is (maybe) relevant for small sites. But probably not for sites with a thousand pages. So, I keep my pimped sitemap.xml for my particular site.

I wonder if maybe sorting is somehow cached. You would sort items for the list pages display anyway, so if Hugo somehow caches this sorted display it might be reused somehow.

1 Like

I am unable to reproduce this. The sitemap ranges through .Pages using the default sort order; this is not an indeterminate sort.

Yes, the sort order will change if you modify weight, date, title/linkTitle, or the path. As you suggest, we could prevent the sort order from changing if we sorted by .Permalink, but the sitemap would still change because it contains a lastmod timestamp.

Your answer is very helpful. It was the parameter weight that had caused the problem that made me think of a random order.

Indeed, a page whose content does not change at all, its URL neither and its date neither (if date and lastmod are manually filled) can change place in the sitemap.xml because of weight.

It’s not a problem at all, but now I know why! Thank you very much.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.