From 8e0bc0257d34d16650e4be5251d35e32fc86b825 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Tue, 22 Jul 2014 11:21:39 -0400 Subject: [PATCH] fixed #1869 --- src/sitemap.js | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/sitemap.js b/src/sitemap.js index 7ce531ae56..6f75486ba2 100644 --- a/src/sitemap.js +++ b/src/sitemap.js @@ -9,6 +9,7 @@ var path = require('path'), topics = require('./topics'), utils = require('../public/src/utils'), sitemap = { + obj: undefined, getStaticUrls: function(callback) { callback(null, [{ url: '', @@ -74,17 +75,28 @@ var path = require('path'), }); }, render: function(callback) { - async.parallel([sitemap.getStaticUrls, sitemap.getDynamicUrls], function(err, urls) { - urls = urls[0].concat(urls[1]); - var map = sm.createSitemap({ + var returnSitemap = function() { + sitemap.obj.toXML(function(xml) { + callback(xml); + }); + }; + + if (sitemap.obj !== undefined && sitemap.obj.cache.length) { + console.log('using sitemap from cache!'); + returnSitemap(); + } else { + console.log('generating new sitemap!', sitemap.obj); + async.parallel([sitemap.getStaticUrls, sitemap.getDynamicUrls], function(err, urls) { + urls = urls[0].concat(urls[1]); + sitemap.obj = sm.createSitemap({ hostname: nconf.get('url'), - cacheTime: 60*60, // Cached for 1 hour + cacheTime: 1000 * 60 * 60, // Cached for 1 hour urls: urls - }), - xml = map.toXML(function(xml) { - callback(xml); }); - }); + + returnSitemap(); + }); + } } };