Skip to content

Commit

Permalink
🐛 fix direct update
Browse files Browse the repository at this point in the history
closes TryGhost#7297
- move sitemap initialisation into sitemap handler
- initialise sitemap on first request to sitemap
  • Loading branch information
kirrg001 committed Sep 5, 2016
1 parent 0b6459c commit 0e4933c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
17 changes: 15 additions & 2 deletions core/server/data/xml/sitemap/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = function handler(blogApp) {
res.send(sitemap.getIndexXml());
});

blogApp.get('/sitemap-:resource.xml', verifyResourceType, function sitemapResourceXML(req, res) {
blogApp.get('/sitemap-:resource.xml', verifyResourceType, function sitemapResourceXML(req, res, next) {
var type = req.params.resource,
page = 1,
siteMapXml = getResourceSiteMapXml(type, page);
Expand All @@ -33,6 +33,19 @@ module.exports = function handler(blogApp) {
'Cache-Control': 'public, max-age=' + utils.ONE_HOUR_S,
'Content-Type': 'text/xml'
});
res.send(siteMapXml);

// CASE: returns null if sitemap is not initialized
if (!siteMapXml) {
sitemap.init()
.then(function () {
siteMapXml = getResourceSiteMapXml(type, page);
res.send(siteMapXml);
})
.catch(function (err) {
next(err);
});
} else {
res.send(siteMapXml);
}
});
};
7 changes: 4 additions & 3 deletions core/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var express = require('express'),
models = require('./models'),
permissions = require('./permissions'),
apps = require('./apps'),
sitemap = require('./data/xml/sitemap'),
xmlrpc = require('./data/xml/xmlrpc'),
slack = require('./data/slack'),
GhostServer = require('./ghost-server'),
Expand Down Expand Up @@ -86,6 +85,10 @@ function init(options) {
}).then(function () {
config.maintenance.enabled = maintenanceState;
}).catch(function (err) {
if (!err) {
return;
}

errors.logErrorAndExit(err, err.context, err.help);
});
} else if (response.error) {
Expand Down Expand Up @@ -115,8 +118,6 @@ function init(options) {
initDbHashAndFirstRun(),
// Initialize apps
apps.init(),
// Initialize sitemaps
sitemap.init(),
// Initialize xmrpc ping
xmlrpc.listen(),
// Initialize slack ping
Expand Down
2 changes: 0 additions & 2 deletions core/test/unit/server_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var should = require('should'),
api = require(config.paths.corePath + '/server/api'),
apps = require(config.paths.corePath + '/server/apps'),
i18n = require(config.paths.corePath + '/server/i18n'),
sitemap = require(config.paths.corePath + '/server/data/xml/sitemap'),
xmlrpc = require(config.paths.corePath + '/server/data/xml/xmlrpc'),
slack = require(config.paths.corePath + '/server/data/slack'),
scheduling = require(config.paths.corePath + '/server/scheduling'),
Expand All @@ -37,7 +36,6 @@ describe('server bootstrap', function () {
sandbox.stub(apps, 'init').returns(Promise.resolve());
sandbox.stub(slack, 'listen').returns(Promise.resolve());
sandbox.stub(xmlrpc, 'listen').returns(Promise.resolve());
sandbox.stub(sitemap, 'init').returns(Promise.resolve());
sandbox.stub(scheduling, 'init').returns(Promise.resolve());

resetMiddlewareStub = bootstrap.__set__('middleware', middlewareStub);
Expand Down

0 comments on commit 0e4933c

Please sign in to comment.