Skip to content

Commit 4c0c120

Browse files
committed
refactor(sitemap): make the plain sitemap dynamic
Signed-off-by: Jayne Doe <[email protected]>
1 parent be6e5fe commit 4c0c120

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

src/journey/seo/sitemap.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,39 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23-
const fs = require('fs');
24-
const path = require('path');
23+
const config = require('config');
24+
const errors = require('restify-errors');
25+
26+
const staticHandlerInstance = require('../../lib/StaticHandler').getHandler();
27+
const StaticDocumentTypes = require('../../lib/StaticDocumentTypes');
28+
29+
const sitemapSorter = (a, b) => (
30+
// eslint-disable-next-line no-nested-ternary
31+
(a.page_name > b.page_name)
32+
? 1
33+
: ((a.page_name < [ 'page_name' ]) ? -1 : 0));
2534

2635
const displaySitemap = async (req, res, next) => {
27-
const robots = fs.readFileSync(path.join(__dirname, '../../seo/sitemap.txt'));
28-
res.contentType = 'text/plain';
29-
res.header('Content-Type', 'text/plain');
30-
res.send(200, robots);
31-
next();
36+
try {
37+
const baseLink = `${
38+
config.get('app.http2.enabled') ? 'https' : 'http'
39+
}://${config.get('app.hostname')}:${config.get('app.port')}`;
40+
41+
const sitemapItems = await staticHandlerInstance.findStatic(StaticDocumentTypes.SITEMAP);
42+
const sortedSiteMap = ((sitemapItems || {}).content || []).sort(sitemapSorter);
43+
const sitemapText = sortedSiteMap.reduce(
44+
(ongoing, current) => `${ongoing}${baseLink}${current.page_link}\n`,
45+
'',
46+
);
47+
48+
res.contentType = 'text/plain';
49+
res.header('content-type', 'text/plain');
50+
res.send(200, sitemapText);
51+
next();
52+
} catch (caught) {
53+
req.log.warn(`Catch during find static :: ${caught}`);
54+
next(new errors.InternalServerError(caught.message));
55+
}
3256
};
3357

3458
module.exports = displaySitemap;

src/seo/sitemap.txt

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)