diff --git a/src/SitemapRotator.js b/src/SitemapRotator.js index 25e093f..28bc255 100644 --- a/src/SitemapRotator.js +++ b/src/SitemapRotator.js @@ -19,7 +19,16 @@ module.exports = function SitemapRotator( }, []); // adds url to stream - const addURL = (url, depth, lastMod = getCurrentDateTime()) => { + const addURL = (url, depth, lastMod = getCurrentDateTime(), baseUrl) => { + if (baseUrl) { + const urlInstance = new URL(url); + const baseUrlInstance = new URL(baseUrl); + urlInstance.protocol = baseUrlInstance.protocol; + urlInstance.host = baseUrlInstance.host; + urlInstance.port = baseUrlInstance.port; + url = urlInstance.href; + } + const currentDateTime = lastModEnabled ? lastMod : null; // exclude existing sitemap.xml diff --git a/src/index.js b/src/index.js index 6213136..2c9a727 100644 --- a/src/index.js +++ b/src/index.js @@ -29,7 +29,8 @@ module.exports = function SitemapGenerator(uri, opts) { changeFreq: '', priorityMap: [], ignoreAMP: true, - ignore: null + ignore: null, + baseUrl: null }; if (!uri) { @@ -109,7 +110,12 @@ module.exports = function SitemapGenerator(uri, opts) { if (sitemapPath !== null) { // eslint-disable-next-line const lastMod = queueItem.stateData.headers['last-modified']; - sitemap.addURL(url, depth, lastMod && format(lastMod, 'YYYY-MM-DD')); + sitemap.addURL( + url, + depth, + lastMod && format(lastMod, 'YYYY-MM-DD'), + opts.baseUrl + ); } } });