Skip to content

Commit

Permalink
feat: add url prefix (#70)
Browse files Browse the repository at this point in the history
* feat: adding ability to set url prefix

A new option has been added to allow us to set a URL prefix this is useful for
when you have a site that want to sit on a url like `http://example.com/en/`

* test: adding test to for urlPrefix

Added test for the urlPrefix also noticed that I neeeded to add a conditional check for when to add
the prefix

* fix: forgot to add the new input to the manifest

* fix: double trailing slash for index url

If the urlPrefix contains a trailing slash ie `/en/` then when we create the
sitemap item for the index `/` the URL would end up with a double trailing slash
`/en//` so we need to replace this with a single slash
  • Loading branch information
aamortimer authored Mar 24, 2021
1 parent fca7f22 commit e30fc70
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,34 @@ package = "@netlify/plugin-sitemap"
changeFreq = "daily"
priority = 0.5
```

### Set base URL from environment variable rather than plugin input

You can include an environment variable (`NETLIFY_PLUGIN_SITEMAP_BASEURL`) in your Netlify site to set the base URL that will be used by the plugin. This option is useful if the `baseUrl` plugin input can't be used.
Example use case: different Netlify sites built from the same repository and don't/can't have custom domains.

Priority of base URL assignment:
plugin input `baseUrl` -> env `NETLIFY_PLUGIN_SITEMAP_BASEURL` -> Netlify site default URL

```toml
[[plugins]]
package = "@netlify/plugin-sitemap"

[plugins.inputs]
baseUrl = "http://example.com"
```

> NOTE: Although the above is called base URL this actually ends up being the hostname in the sitemap and as such trying to use a URL like `http://example.com/en/` will results in `http://example.com/`
### Add a prefix to the URL

You can include an environment variable (NETLIFY_PLUGIN_SITEMAP_URL_PREFIX) in your Netlify site to set the URL prefix that will be used by the plugin. This option is useful if the urlPrefix plugin input can't be used. Example use case: different Netlify sites built from the same repository and don't/can't have custom domains.

Priority of base URL assignment: plugin input urlPrefix -> env NETLIFY_PLUGIN_SITEMAP_URL_PREFIX

```toml
[[plugins]]
package = "@netlify/plugin-sitemap"

[plugins.inputs]
urlPrefix = "/en/"
```
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const getBuildDir = ({ inputs, constants }) => {
module.exports = {
onPostBuild: async ({ constants, inputs, utils }) => {
const baseUrl = inputs.baseUrl || env.NETLIFY_PLUGIN_SITEMAP_BASEURL || env.URL
const urlPrefix = inputs.urlPrefix || env.NETLIFY_PLUGIN_SITEMAP_URL_PREFIX || null
const buildDir = getBuildDir({ inputs, constants })

console.log('Creating sitemap from files...')
Expand All @@ -34,6 +35,7 @@ module.exports = {
priority: inputs.priority,
trailingSlash: inputs.trailingSlash,
failBuild: utils.build.failBuild,
urlPrefix,
})

console.log('Sitemap Built!', data.sitemapPath)
Expand Down
8 changes: 5 additions & 3 deletions make_sitemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ const getUrlFromFile = ({ file, distPath, prettyURLs, trailingSlash }) => {
return prettyUrl
}

const getUrlsFromPaths = ({ paths, distPath, prettyURLs, trailingSlash, changeFreq, priority, cwd }) => {
const getUrlsFromPaths = ({ paths, distPath, prettyURLs, trailingSlash, changeFreq, priority, cwd, urlPrefix }) => {
const urls = paths.map((file) => {
const url = getUrlFromFile({ file, distPath, prettyURLs, trailingSlash })

return {
url,
url: (urlPrefix ? urlPrefix + url : url).replace('//', '/'),
changefreq: changeFreq,
priority,
lastmodrealtime: true,
Expand Down Expand Up @@ -83,6 +84,7 @@ module.exports = async function makeSitemap(opts = {}) {
distPath,
fileName,
homepage,
urlPrefix,
exclude,
prettyURLs,
trailingSlash,
Expand All @@ -93,7 +95,7 @@ module.exports = async function makeSitemap(opts = {}) {
} = opts

const paths = await getPaths({ distPath, exclude, cwd })
const urls = getUrlsFromPaths({ paths, distPath, prettyURLs, trailingSlash, changeFreq, priority, cwd })
const urls = getUrlsFromPaths({ paths, distPath, prettyURLs, trailingSlash, changeFreq, priority, cwd, urlPrefix })

const { sitemap, xml } = await createSitemapInfo({ homepage, urls, failBuild })

Expand Down
3 changes: 3 additions & 0 deletions manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ inputs:
- name: trailingSlash
# Append trailing slash to pretty URL
default: false
- name: urlPrefix
# Prefix the url with a value such as /en/
default: null
27 changes: 27 additions & 0 deletions tests/sitemap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ CONFIGS.forEach(({ distPath, testNamePostfix, cwd, excludePath }) => {
const xmlData = await parseXml(fileName)
const pages = getPages(xmlData)
t.truthy(sitemapData.sitemapPath)

t.deepEqual(pages, [
'https://site.com/',
'https://site.com/page-one',
Expand All @@ -188,6 +189,32 @@ CONFIGS.forEach(({ distPath, testNamePostfix, cwd, excludePath }) => {
// excluded 'https://site.com/children/grandchildren/grandchild-two.html'
])
})

test(`Sitemap urlPrefix works correctly - ${testNamePostfix}`, async (t) => {
const { fileName } = t.context
const sitemapData = await makeSitemap({
homepage: 'https://site.com/',
distPath,
prettyURLs: true,
urlPrefix: 'en/',
failBuild() {},
fileName,
cwd,
})
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',
])
})
})

const getPages = (data) => data.urlset.url.map((record) => record.loc[0])
Expand Down

0 comments on commit e30fc70

Please sign in to comment.