Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding baseUrl option and logic for #65 #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/SitemapRotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ module.exports = function SitemapGenerator(uri, opts) {
changeFreq: '',
priorityMap: [],
ignoreAMP: true,
ignore: null
ignore: null,
baseUrl: null
};

if (!uri) {
Expand Down Expand Up @@ -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
);
}
}
});
Expand Down