Skip to content
Permalink
Browse files

feat: add "trailingSlash" option to add a trailing slash to each route

fix #34 #78
  • Loading branch information...
NicoPennec committed Aug 22, 2019
1 parent 47ed8bd commit b82bb668df5a8fb04f641c4c13674a38ed9cebd2
Showing with 38 additions and 0 deletions.
  1. +7 −0 README.md
  2. +9 −0 lib/module.js
  3. +1 −0 test/fixture/nuxt.config.js
  4. +21 −0 test/module.test.js
@@ -150,6 +150,13 @@ Set the XML namespaces by override all default `xmlns` attributes in `<urlset>`
The URL path of the XSL file to style the sitemap.
### `trailingSlash` (optional)
- Default: `false`
Add a trailing slash to each route URL (eg. `/page/1` => `/page/1/`)
> **notice:** To avoid [duplicate content](https://support.google.com/webmasters/answer/66359) detection from crawlers, you have to configure a HTTP 301 redirect between the 2 URLs (see [redirect-module](https://github.com/nuxt-community/redirect-module) or [nuxt-trailingslash-module](https://github.com/WilliamDASILVA/nuxt-trailingslash-module)).
### `defaults` (optional)
- Default: `{}`
@@ -23,6 +23,7 @@ module.exports = function module(moduleOptions) {
gzip: false,
xmlNs: undefined,
xslUrl: undefined,
trailingSlash: false,
defaults: {}
}

@@ -173,6 +174,14 @@ function createSitemap(options, routes, req) {

routes = routes.map(route => ({ ...options.defaults, ...route }))

// Add a trailing slash to each route URL
if (options.trailingSlash) {
routes = routes.map(route => {
if (!route.url.endsWith('/')) route.url = `${route.url}/`
return route
})
}

// Enable filter function for each declared route
if (typeof options.filter === 'function') {
routes = options.filter({
@@ -13,6 +13,7 @@ module.exports = {
filter: ({ routes }) => routes.filter(route => route.url !== '/filtered'),
xmlNs: 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"',
// xslUrl: 'sitemap.xsl',
trailingSlash: true,
defaults: {
changefreq: 'daily',
priority: 1
@@ -186,6 +186,27 @@ describe('sitemap - advanced configuration', () => {
const sitemap = gunzipSync(gz).toString()
expect(xml).toEqual(sitemap)
})

test('trailingSlash enabled', async () => {
nuxt = await startServer({
...config,
sitemap: {
hostname: 'https://example.com',
trailingSlash: true,
routes: ['test']
}
})

const xml = await get('/sitemap.xml')

// trailing slash
expect(xml).not.toContain('<loc>https://example.com/sub</loc>')
expect(xml).not.toContain('<loc>https://example.com/sub/sub</loc>')
expect(xml).not.toContain('<loc>https://example.com/test</loc>')
expect(xml).toContain('<loc>https://example.com/sub/</loc>')
expect(xml).toContain('<loc>https://example.com/sub/sub/</loc>')
expect(xml).toContain('<loc>https://example.com/test/</loc>')
})
})

describe('external options', () => {

0 comments on commit b82bb66

Please sign in to comment.
You can’t perform that action at this time.