kjvarga/sitemap_generator

add_to_index overwrites index file

wordjelly opened this issue · 2 comments

Hi,
I have a situation where I wish to "update" the sitemap index file, adding more links to it,over time. We have a lot of dynamic content generation on our site.

When I do the following:

SitemapGenerator::Sitemap.create do
    add_to_index "/new_sitemap.xml.gz"
end

I find that it overwrites the old sitemap index file.
Any links that were present in the older sitemap index are gone,and now I only have one link with "new_sitemap.xml.gz" in the index.

How do I update the index file, so that it preserves older links?

Thanks.

@wordjelly there is no mechanism for updating a sitemap. Because sitemaps are XML files, there is no easy way to do that. You could write something yourself, but the general approach is to just regenerate your entire sitemap periodically (as often as you need to). That is the simplest approach. Sometimes people will generate sitemap files for only the new content since the last run (and ensure that the new files don't replace existing ones), then regenerate only the index by adding links for each of the old sitemaps, and the new sitemap(s). Otherwise you will have to parse the XML index file, add the new links and rewrite the file.

Unless you have millions of links and/or are needing to add content multiple times a day, by far the simplest is to just regenerate each time.

Would you be open to a PR on this ? I thought, just like "add_to_index" , we could have an API method "update_index". Let me know and I'll close this.