diff --git a/core/server/data/xml/sitemap/handler.js b/core/server/data/xml/sitemap/handler.js index 8432e6e593..cc7d99aea9 100644 --- a/core/server/data/xml/sitemap/handler.js +++ b/core/server/data/xml/sitemap/handler.js @@ -16,12 +16,27 @@ module.exports = function handler(blogApp) { return sitemap.getSiteMapXml(type, page); }; - blogApp.get('/sitemap.xml', function sitemapXML(req, res) { + blogApp.get('/sitemap.xml', function sitemapXML(req, res, next) { + var siteMapXml = sitemap.getIndexXml(); + res.set({ 'Cache-Control': 'public, max-age=' + utils.ONE_HOUR_S, 'Content-Type': 'text/xml' }); - res.send(sitemap.getIndexXml()); + + // CASE: returns null if sitemap is not initialized as below + if (!siteMapXml) { + sitemap.init() + .then(function () { + siteMapXml = sitemap.getIndexXml(); + res.send(siteMapXml); + }) + .catch(function (err) { + next(err); + }); + } else { + res.send(siteMapXml); + } }); blogApp.get('/sitemap-:resource.xml', verifyResourceType, function sitemapResourceXML(req, res, next) { diff --git a/core/test/functional/routes/frontend_spec.js b/core/test/functional/routes/frontend_spec.js index 3fab481d14..bee3def6bd 100644 --- a/core/test/functional/routes/frontend_spec.js +++ b/core/test/functional/routes/frontend_spec.js @@ -683,7 +683,10 @@ describe('Frontend Routing', function () { .expect(200) .expect('Cache-Control', testUtils.cacheRules.hour) .expect('Content-Type', 'text/xml; charset=utf-8') - .end(doEnd(done)); + .end(function (err, res) { + res.text.should.match(/sitemapindex/); + doEnd(done)(err, res); + }); }); it('should serve sitemap-posts.xml', function (done) { @@ -691,7 +694,10 @@ describe('Frontend Routing', function () { .expect(200) .expect('Cache-Control', testUtils.cacheRules.hour) .expect('Content-Type', 'text/xml; charset=utf-8') - .end(doEnd(done)); + .end(function (err, res) { + res.text.should.match(/urlset/); + doEnd(done)(err, res); + }); }); it('should serve sitemap-pages.xml', function (done) { @@ -699,7 +705,10 @@ describe('Frontend Routing', function () { .expect(200) .expect('Cache-Control', testUtils.cacheRules.hour) .expect('Content-Type', 'text/xml; charset=utf-8') - .end(doEnd(done)); + .end(function (err, res) { + res.text.should.match(/urlset/); + doEnd(done)(err, res); + }); }); // TODO: Other pages and verify content