Skip to content

Commit a49cc1b

Browse files
committed
fix: add check for missing or invalid URL creating a sitemap stream
1 parent c6ad679 commit a49cc1b

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

server/services/core.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const { getConfigUrls } = require('@strapi/utils');
88
const { SitemapStream, streamToPromise, SitemapAndIndexStream } = require('sitemap');
99
const { isEmpty } = require('lodash');
1010

11-
const { logMessage, getService, formatCache, mergeCache } = require('../utils');
11+
const { logMessage, getService, formatCache, mergeCache, isValidUrl } = require('../utils');
1212

1313
/**
1414
* Add link x-default url to url bundles from strapi i18n plugin default locale.
@@ -243,6 +243,16 @@ const getSitemapStream = async (urlCount) => {
243243
xslObj.xslUrl = 'xsl/sitemap.xsl';
244244
}
245245

246+
if (!config.hostname) {
247+
strapi.log.info(logMessage('No sitemap XML was generated because there was no hostname configured.'));
248+
return;
249+
}
250+
251+
if (!isValidUrl(config.hostname)) {
252+
strapi.log.info(logMessage('No sitemap XML was generated because the hostname was invalid'));
253+
return;
254+
}
255+
246256
if (urlCount <= LIMIT) {
247257
return [new SitemapStream({
248258
hostname: config.hostname,

server/utils/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,23 @@ const mergeCache = (oldCache, newCache) => {
7171
return mergedCache;
7272
};
7373

74+
75+
const isValidUrl = (url) => {
76+
try {
77+
// eslint-disable-next-line no-new
78+
new URL(!url);
79+
return true;
80+
} catch (err) {
81+
return false;
82+
}
83+
};
84+
7485
module.exports = {
7586
getService,
7687
getCoreStore,
7788
logMessage,
7889
noLimit,
7990
formatCache,
8091
mergeCache,
92+
isValidUrl,
8193
};

0 commit comments

Comments
 (0)