feat: upgrade to sitemap5 (#7980)

v1.18.x
Barış Soner Uşaklı 5 years ago committed by GitHub
parent 73e7aec5a4
commit d679218859
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -110,7 +110,7 @@
"semver": "^6.0.0", "semver": "^6.0.0",
"serve-favicon": "^2.4.5", "serve-favicon": "^2.4.5",
"sharp": "0.23.1", "sharp": "0.23.1",
"sitemap": "^4.0.0", "sitemap": "^5.0.0",
"socket.io": "2.3.0", "socket.io": "2.3.0",
"socket.io-adapter-cluster": "^1.0.1", "socket.io-adapter-cluster": "^1.0.1",
"socket.io-adapter-mongo": "^2.0.4", "socket.io-adapter-mongo": "^2.0.4",

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const { Sitemap } = require('sitemap'); const { SitemapStream, streamToPromise } = require('sitemap');
const nconf = require('nconf'); const nconf = require('nconf');
const db = require('./database'); const db = require('./database');
@ -32,11 +32,8 @@ sitemap.render = async function () {
}; };
sitemap.getPages = async function () { sitemap.getPages = async function () {
if ( if (sitemap.maps.pages && Date.now() < sitemap.maps.pagesCacheExpireTimestamp) {
sitemap.maps.pages && return sitemap.maps.pages.toString();
Date.now() < parseInt(sitemap.maps.pages.cacheSetTimestamp, 10) + parseInt(sitemap.maps.pages.cacheResetPeriod, 10)
) {
return sitemap.maps.pages.toXML();
} }
const urls = [{ const urls = [{
@ -58,21 +55,19 @@ sitemap.getPages = async function () {
}]; }];
const data = await plugins.fireHook('filter:sitemap.getPages', { urls: urls }); const data = await plugins.fireHook('filter:sitemap.getPages', { urls: urls });
sitemap.maps.pages = new Sitemap({
hostname: nconf.get('url'),
cacheTime: 1000 * 60 * 60 * 24, // Cached for 24 hours
urls: data.urls,
});
return sitemap.maps.pages.toXML(); const smStream = new SitemapStream({ hostname: nconf.get('url') });
data.urls.forEach(url => smStream.write(url));
smStream.end();
sitemap.maps.pages = await streamToPromise(smStream);
sitemap.maps.pagesCacheExpireTimestamp = Date.now() + (1000 * 60 * 60 * 24);
return sitemap.maps.pages.toString();
}; };
sitemap.getCategories = async function () { sitemap.getCategories = async function () {
if ( if (sitemap.maps.categories && Date.now() < sitemap.maps.categoriesCacheExpireTimestamp) {
sitemap.maps.categories && return sitemap.maps.categories.toString();
Date.now() < parseInt(sitemap.maps.categories.cacheSetTimestamp, 10) + parseInt(sitemap.maps.categories.cacheResetPeriod, 10)
) {
return sitemap.maps.categories.toXML();
} }
const categoryUrls = []; const categoryUrls = [];
@ -87,13 +82,13 @@ sitemap.getCategories = async function () {
} }
}); });
sitemap.maps.categories = new Sitemap({ const smStream = new SitemapStream({ hostname: nconf.get('url') });
hostname: nconf.get('url'), categoryUrls.forEach(url => smStream.write(url));
cacheTime: 1000 * 60 * 60 * 24, // Cached for 24 hours smStream.end();
urls: categoryUrls,
});
return sitemap.maps.categories.toXML(); sitemap.maps.categories = await streamToPromise(smStream);
sitemap.maps.categoriesCacheExpireTimestamp = Date.now() + (1000 * 60 * 60 * 24);
return sitemap.maps.categories.toString();
}; };
sitemap.getTopicPage = async function (page) { sitemap.getTopicPage = async function (page) {
@ -105,11 +100,8 @@ sitemap.getTopicPage = async function (page) {
const min = (parseInt(page, 10) - 1) * numTopics; const min = (parseInt(page, 10) - 1) * numTopics;
const max = min + numTopics; const max = min + numTopics;
if ( if (sitemap.maps.topics[page - 1] && Date.now() < sitemap.maps.topics[page - 1].cacheExpireTimestamp) {
sitemap.maps.topics[page - 1] && return sitemap.maps.topics[page - 1].sm.toString();
Date.now() < parseInt(sitemap.maps.topics[page - 1].cacheSetTimestamp, 10) + parseInt(sitemap.maps.topics[page - 1].cacheResetPeriod, 10)
) {
return sitemap.maps.topics[page - 1].toXML();
} }
const topicUrls = []; const topicUrls = [];
@ -128,19 +120,28 @@ sitemap.getTopicPage = async function (page) {
} }
}); });
sitemap.maps.topics[page - 1] = new Sitemap({ const smStream = new SitemapStream({ hostname: nconf.get('url') });
hostname: nconf.get('url'), topicUrls.forEach(url => smStream.write(url));
cacheTime: 1000 * 60 * 60, // Cached for 1 hour smStream.end();
urls: topicUrls,
});
return sitemap.maps.topics[page - 1].toXML(); sitemap.maps.topics[page - 1] = {
sm: await streamToPromise(smStream),
cacheExpireTimestamp: Date.now() + (1000 * 60 * 60 * 24),
};
return sitemap.maps.topics[page - 1].sm.toString();
}; };
sitemap.clearCache = function () { sitemap.clearCache = function () {
if (sitemap.obj) { if (sitemap.maps.pages) {
sitemap.obj.clearCache(); sitemap.maps.pagesCacheExpireTimestamp = 0;
} }
if (sitemap.maps.categories) {
sitemap.maps.categoriesCacheExpireTimestamp = 0;
}
sitemap.maps.topics.forEach((topicMap) => {
topicMap.cacheExpireTimestamp = 0;
});
}; };
require('./promisify')(sitemap); require('./promisify')(sitemap);

Loading…
Cancel
Save