Skip to content

Commit e30fc70

Browse files
authored
feat: add url prefix (#70)
* 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
1 parent fca7f22 commit e30fc70

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,34 @@ package = "@netlify/plugin-sitemap"
102102
changeFreq = "daily"
103103
priority = 0.5
104104
```
105-
106105
### Set base URL from environment variable rather than plugin input
107106

108107
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.
109108
Example use case: different Netlify sites built from the same repository and don't/can't have custom domains.
110109

111110
Priority of base URL assignment:
112111
plugin input `baseUrl` -> env `NETLIFY_PLUGIN_SITEMAP_BASEURL` -> Netlify site default URL
112+
113+
```toml
114+
[[plugins]]
115+
package = "@netlify/plugin-sitemap"
116+
117+
[plugins.inputs]
118+
baseUrl = "http://example.com"
119+
```
120+
121+
> 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/`
122+
123+
### Add a prefix to the URL
124+
125+
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.
126+
127+
Priority of base URL assignment: plugin input urlPrefix -> env NETLIFY_PLUGIN_SITEMAP_URL_PREFIX
128+
129+
```toml
130+
[[plugins]]
131+
package = "@netlify/plugin-sitemap"
132+
133+
[plugins.inputs]
134+
urlPrefix = "/en/"
135+
```

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const getBuildDir = ({ inputs, constants }) => {
2020
module.exports = {
2121
onPostBuild: async ({ constants, inputs, utils }) => {
2222
const baseUrl = inputs.baseUrl || env.NETLIFY_PLUGIN_SITEMAP_BASEURL || env.URL
23+
const urlPrefix = inputs.urlPrefix || env.NETLIFY_PLUGIN_SITEMAP_URL_PREFIX || null
2324
const buildDir = getBuildDir({ inputs, constants })
2425

2526
console.log('Creating sitemap from files...')
@@ -34,6 +35,7 @@ module.exports = {
3435
priority: inputs.priority,
3536
trailingSlash: inputs.trailingSlash,
3637
failBuild: utils.build.failBuild,
38+
urlPrefix,
3739
})
3840

3941
console.log('Sitemap Built!', data.sitemapPath)

make_sitemap.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ const getUrlFromFile = ({ file, distPath, prettyURLs, trailingSlash }) => {
4646
return prettyUrl
4747
}
4848

49-
const getUrlsFromPaths = ({ paths, distPath, prettyURLs, trailingSlash, changeFreq, priority, cwd }) => {
49+
const getUrlsFromPaths = ({ paths, distPath, prettyURLs, trailingSlash, changeFreq, priority, cwd, urlPrefix }) => {
5050
const urls = paths.map((file) => {
5151
const url = getUrlFromFile({ file, distPath, prettyURLs, trailingSlash })
52+
5253
return {
53-
url,
54+
url: (urlPrefix ? urlPrefix + url : url).replace('//', '/'),
5455
changefreq: changeFreq,
5556
priority,
5657
lastmodrealtime: true,
@@ -83,6 +84,7 @@ module.exports = async function makeSitemap(opts = {}) {
8384
distPath,
8485
fileName,
8586
homepage,
87+
urlPrefix,
8688
exclude,
8789
prettyURLs,
8890
trailingSlash,
@@ -93,7 +95,7 @@ module.exports = async function makeSitemap(opts = {}) {
9395
} = opts
9496

9597
const paths = await getPaths({ distPath, exclude, cwd })
96-
const urls = getUrlsFromPaths({ paths, distPath, prettyURLs, trailingSlash, changeFreq, priority, cwd })
98+
const urls = getUrlsFromPaths({ paths, distPath, prettyURLs, trailingSlash, changeFreq, priority, cwd, urlPrefix })
9799

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

manifest.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ inputs:
2121
- name: trailingSlash
2222
# Append trailing slash to pretty URL
2323
default: false
24+
- name: urlPrefix
25+
# Prefix the url with a value such as /en/
26+
default: null

tests/sitemap.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ CONFIGS.forEach(({ distPath, testNamePostfix, cwd, excludePath }) => {
177177
const xmlData = await parseXml(fileName)
178178
const pages = getPages(xmlData)
179179
t.truthy(sitemapData.sitemapPath)
180+
180181
t.deepEqual(pages, [
181182
'https://site.com/',
182183
'https://site.com/page-one',
@@ -188,6 +189,32 @@ CONFIGS.forEach(({ distPath, testNamePostfix, cwd, excludePath }) => {
188189
// excluded 'https://site.com/children/grandchildren/grandchild-two.html'
189190
])
190191
})
192+
193+
test(`Sitemap urlPrefix works correctly - ${testNamePostfix}`, async (t) => {
194+
const { fileName } = t.context
195+
const sitemapData = await makeSitemap({
196+
homepage: 'https://site.com/',
197+
distPath,
198+
prettyURLs: true,
199+
urlPrefix: 'en/',
200+
failBuild() {},
201+
fileName,
202+
cwd,
203+
})
204+
const xmlData = await parseXml(fileName)
205+
const pages = getPages(xmlData)
206+
t.truthy(sitemapData.sitemapPath)
207+
t.deepEqual(pages, [
208+
'https://site.com/en/',
209+
'https://site.com/en/page-one',
210+
'https://site.com/en/page-three',
211+
'https://site.com/en/page-two',
212+
'https://site.com/en/children/child-one',
213+
'https://site.com/en/children/child-two',
214+
'https://site.com/en/children/grandchildren/grandchild-one',
215+
'https://site.com/en/children/grandchildren/grandchild-two',
216+
])
217+
})
191218
})
192219

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

0 commit comments

Comments
 (0)