Skip to content
Draft
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: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
```
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = {
trailingSlash: inputs.trailingSlash,
failBuild: utils.build.failBuild,
urlPrefix,
customUrls: inputs.customUrls,
})

console.log('Sitemap Built!', data.sitemapPath)
Expand Down
35 changes: 33 additions & 2 deletions make_sitemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

Expand All @@ -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
}

Expand Down Expand Up @@ -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 })

Expand Down
29 changes: 29 additions & 0 deletions tests/sitemap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down