diff --git a/README.md b/README.md index bc62dc4..a7dd1c2 100644 --- a/README.md +++ b/README.md @@ -133,3 +133,14 @@ package = "@netlify/plugin-sitemap" [plugins.inputs] urlPrefix = "/en/" ``` +### Add custom URLs to the Sitemap + +You can include custom URLs to be added to the sitemap by passing an array of custom URLs. + +```toml +[[plugins]] +package = "@netlify/plugin-sitemap" + + [plugins.inputs] + customUrls = ["/news", "/about"] +``` diff --git a/index.js b/index.js index 01520a3..48fee55 100644 --- a/index.js +++ b/index.js @@ -36,6 +36,7 @@ module.exports = { trailingSlash: inputs.trailingSlash, failBuild: utils.build.failBuild, urlPrefix, + customUrls: inputs.customUrls, }) console.log('Sitemap Built!', data.sitemapPath) diff --git a/make_sitemap.js b/make_sitemap.js index 3b1be84..5674727 100644 --- a/make_sitemap.js +++ b/make_sitemap.js @@ -47,7 +47,17 @@ const getUrlFromFile = ({ file, distPath, prettyURLs, trailingSlash }) => { return prettyUrl } -const getUrlsFromPaths = ({ paths, distPath, prettyURLs, trailingSlash, changeFreq, priority, cwd, urlPrefix }) => { +const getUrlsFromPaths = ({ + paths, + distPath, + prettyURLs, + trailingSlash, + changeFreq, + priority, + cwd, + urlPrefix, + customUrls, +}) => { const urls = paths.map((file) => { const url = getUrlFromFile({ file, distPath, prettyURLs, trailingSlash }) @@ -59,6 +69,16 @@ const getUrlsFromPaths = ({ paths, distPath, prettyURLs, trailingSlash, changeFr lastmodfile: cwd === undefined ? file : path.resolve(cwd, file), } }) + customUrls.forEach((customUrl) => { + const url = { + url: (urlPrefix ? urlPrefix + customUrl : customUrl).replace('//', '/'), + changefreq: changeFreq, + priority, + lastmodrealtime: true, + } + // eslint-disable-next-line fp/no-mutating-methods + urls.push(url) + }) return urls } @@ -93,10 +113,21 @@ module.exports = async function makeSitemap(opts = {}) { cwd, changeFreq = DEFAULT_CHANGE_FREQ, priority = DEFAULT_PRIORITY, + customUrls = [], } = opts const paths = await getPaths({ distPath, exclude, cwd }) - const urls = getUrlsFromPaths({ paths, distPath, prettyURLs, trailingSlash, changeFreq, priority, cwd, urlPrefix }) + const urls = getUrlsFromPaths({ + paths, + distPath, + prettyURLs, + trailingSlash, + changeFreq, + priority, + cwd, + urlPrefix, + customUrls, + }) const { sitemap, xml } = await createSitemapInfo({ homepage, urls, failBuild }) diff --git a/tests/sitemap.test.js b/tests/sitemap.test.js index 6dc85e3..32ea86b 100644 --- a/tests/sitemap.test.js +++ b/tests/sitemap.test.js @@ -215,6 +215,35 @@ CONFIGS.forEach(({ distPath, testNamePostfix, cwd, excludePath }) => { 'https://site.com/en/children/grandchildren/grandchild-two', ]) }) + + test(`Generate Sitemap with custom URLs - ${testNamePostfix}`, async (t) => { + const { fileName } = t.context + const sitemapData = await makeSitemap({ + homepage: 'https://site.com/', + distPath, + prettyURLs: true, + urlPrefix: 'en/', + failBuild() {}, + fileName, + cwd, + customUrls: ['/news', '/about'], + }) + const xmlData = await parseXml(fileName) + const pages = getPages(xmlData) + t.truthy(sitemapData.sitemapPath) + t.deepEqual(pages, [ + 'https://site.com/en/', + 'https://site.com/en/page-one', + 'https://site.com/en/page-three', + 'https://site.com/en/page-two', + 'https://site.com/en/children/child-one', + 'https://site.com/en/children/child-two', + 'https://site.com/en/children/grandchildren/grandchild-one', + 'https://site.com/en/children/grandchildren/grandchild-two', + 'https://site.com/en/news', + 'https://site.com/en/about', + ]) + }) }) const getPages = (data) => data.urlset.url.map((record) => record.loc[0])