2014-12-01 13:43:01 +03:00
|
|
|
var _ = require('lodash'),
|
2017-05-29 23:10:32 +03:00
|
|
|
config = require('../../../config'),
|
2014-10-28 03:41:18 +03:00
|
|
|
sitemap = require('./index');
|
|
|
|
|
|
|
|
// Responsible for handling requests for sitemap files
|
2017-10-26 19:24:08 +03:00
|
|
|
module.exports = function handler(siteApp) {
|
2014-10-28 03:41:18 +03:00
|
|
|
var resourceTypes = ['posts', 'authors', 'tags', 'pages'],
|
2015-05-30 23:18:26 +03:00
|
|
|
verifyResourceType = function verifyResourceType(req, res, next) {
|
2016-06-11 21:23:27 +03:00
|
|
|
if (!_.includes(resourceTypes, req.params.resource)) {
|
2014-12-01 13:43:01 +03:00
|
|
|
return res.sendStatus(404);
|
2014-10-28 03:41:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
},
|
2015-05-30 23:18:26 +03:00
|
|
|
getResourceSiteMapXml = function getResourceSiteMapXml(type, page) {
|
2014-10-28 03:41:18 +03:00
|
|
|
return sitemap.getSiteMapXml(type, page);
|
|
|
|
};
|
|
|
|
|
2017-10-26 19:24:08 +03:00
|
|
|
siteApp.get('/sitemap.xml', function sitemapXML(req, res, next) {
|
2016-09-14 11:44:08 +03:00
|
|
|
var siteMapXml = sitemap.getIndexXml();
|
|
|
|
|
2014-10-28 03:41:18 +03:00
|
|
|
res.set({
|
2017-05-29 23:10:32 +03:00
|
|
|
'Cache-Control': 'public, max-age=' + config.get('caching:sitemap:maxAge'),
|
2014-10-28 03:41:18 +03:00
|
|
|
'Content-Type': 'text/xml'
|
|
|
|
});
|
2016-09-14 11:44:08 +03:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2014-10-28 03:41:18 +03:00
|
|
|
});
|
|
|
|
|
2017-10-26 19:24:08 +03:00
|
|
|
siteApp.get('/sitemap-:resource.xml', verifyResourceType, function sitemapResourceXML(req, res, next) {
|
2015-02-26 10:28:37 +03:00
|
|
|
var type = req.params.resource,
|
2014-10-28 03:41:18 +03:00
|
|
|
page = 1,
|
|
|
|
siteMapXml = getResourceSiteMapXml(type, page);
|
|
|
|
|
|
|
|
res.set({
|
2017-05-29 23:10:32 +03:00
|
|
|
'Cache-Control': 'public, max-age=' + config.get('caching:sitemap:maxAge'),
|
2014-10-28 03:41:18 +03:00
|
|
|
'Content-Type': 'text/xml'
|
|
|
|
});
|
2016-09-06 14:16:32 +03:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2014-10-28 03:41:18 +03:00
|
|
|
});
|
|
|
|
};
|