Question

The documentation of the sitemap node.js module does not explain what cacheTime is. Why is it needed to generate a sitemap? What is its purpose?

Was it helpful?

Solution

The cacheTime is how long the sitemap.js module will wait before regenerating the sitemap.xml file from the list of urls given to it.

ie. on the first request, a sitemap.xml file is generated and placed in the cache. Subsequent requests read the sitemap from the cache, until it expires and is regenerated.

I agree it could be clearer, but the source code makes it pretty clear.

According to the source code at sitemap.js, line 136:

// sitemap cache
  this.cacheEnable = false;
  this.cache = '';
  if (cacheTime > 0) {
    this.cacheEnable = true;
    this.cacheCleanerId = setInterval(function (self) {
      self.clearCache();
    }, cacheTime, this);
  }

and line 187:

Sitemap.prototype.toString = function () {
  var self = this
    , xml = [ '<?xml version="1.0" encoding="UTF-8"?>',
              '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'];

  if (this.cacheEnable && this.cache) {
    return this.cache;
  }

  // TODO: if size > limit: create sitemapindex

  this.urls.forEach( function (elem, index) {
    // SitemapItem
    var smi = elem;

Specifically:

if (this.cacheEnable && this.cache) {
    return this.cache;
}

And the clear cache operation has a setInterval on it equal to the cacheTime parameter given.

Note the implication that your sitemap could become out of date if your urls change and your cacheTime has not triggered a clearing of the sitemap cache.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top