Skip to content

Commit

Permalink
feat: add trailingSlash config
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Dec 13, 2022
1 parent de67e57 commit 0674a18
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ The following options are available for each route rule:

If you need further control over the sitemap URLs, you can provide config on the `sitemap` key.

### `host`

- Type: `string`
- Default: `undefined`
- Required: `true`

The host of your site. This is required to generate the sitemap.xml.

### `trailingSlash`

- Type: `boolean`
- Default: `false`

Whether to add a trailing slash to the URLs in the sitemap.xml.

### `enabled`

- Type: `boolean`
Expand Down
17 changes: 14 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SitemapStream, streamToPromise } from 'sitemap'
import { createRouter as createRadixRouter, toRouteMatcher } from 'radix3'
import type { Nitro } from 'nitropack'
import chalk from 'chalk'
import { withTrailingSlash, withoutTrailingSlash } from 'ufo'
import type { CreateFilterOptions } from './urlFilter'
import { createFilter } from './urlFilter'

Expand All @@ -17,6 +18,12 @@ export interface ModuleOptions extends CreateFilterOptions, SitemapStreamOptions
* @default true
*/
enabled: boolean
/**
* Should the URLs be inserted with a trailing slash.
*
* @default false
*/
trailingSlash: boolean
}

export interface ModuleHooks {
Expand All @@ -36,7 +43,8 @@ export default defineNuxtModule<ModuleOptions>({
defaults(nuxt) {
return {
include: ['/**'],
hostname: nuxt.options.runtimeConfig.siteUrl,
hostname: nuxt.options.runtimeConfig.public?.siteUrl,
trailingSlash: nuxt.options.runtimeConfig.public.trailingSlash,
enabled: true,
}
},
Expand Down Expand Up @@ -96,6 +104,8 @@ declare module 'nitropack' {
const urlFilter = createFilter(config)
const stream = new SitemapStream(config)

const fixSlashes = (url: string) => nuxt.options.schema?.trailingSlash ? withTrailingSlash(url) : withoutTrailingSlash(url)

const urls = sitemapRoutes
// filter for config
.filter(urlFilter)
Expand All @@ -109,8 +119,9 @@ declare module 'nitropack' {
return false

// @ts-expect-error untyped
return { url: path, ...(routeRules.sitemap || {}) }
}).filter(Boolean)
return { url: fixSlashes(path), ...(routeRules.sitemap || {}) }
})
.filter(Boolean)

const sitemapContext = { stream, urls }
// @ts-expect-error untyped
Expand Down
23 changes: 23 additions & 0 deletions test/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,27 @@ describe('generate', () => {
const sitemap = await readFile(resolve(rootDir, '.output/public/sitemap.xml'), 'utf-8')
expect(sitemap).toMatchInlineSnapshot('"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\" xmlns:news=\\"http://www.google.com/schemas/sitemap-news/0.9\\" xmlns:xhtml=\\"http://www.w3.org/1999/xhtml\\" xmlns:image=\\"http://www.google.com/schemas/sitemap-image/1.1\\" xmlns:video=\\"http://www.google.com/schemas/sitemap-video/1.1\\"><url><loc>https://example.com/</loc></url><url><loc>https://example.com/about</loc><changefreq>daily</changefreq><priority>0.3</priority></url><url><loc>https://example.com/hidden-path-but-in-sitemap</loc></url></urlset>"')
}, 60000)

it('trailing slash', async () => {
process.env.NODE_ENV = 'production'
process.env.prerender = true
const { resolve } = createResolver(import.meta.url)
const rootDir = resolve('../.playground')
const nuxt = await loadNuxt({
rootDir,
overrides: {
_generate: true,
},
})

nuxt.options.schema = nuxt.options.schema || {}
nuxt.options.schema.trailingSlash = true

await buildNuxt(nuxt)

await new Promise(resolve => setTimeout(resolve, 1000))

const sitemap = await readFile(resolve(rootDir, '.output/public/sitemap.xml'), 'utf-8')
expect(sitemap).toMatchInlineSnapshot('"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\" xmlns:news=\\"http://www.google.com/schemas/sitemap-news/0.9\\" xmlns:xhtml=\\"http://www.w3.org/1999/xhtml\\" xmlns:image=\\"http://www.google.com/schemas/sitemap-image/1.1\\" xmlns:video=\\"http://www.google.com/schemas/sitemap-video/1.1\\"><url><loc>https://example.com/</loc></url><url><loc>https://example.com/about/</loc><changefreq>daily</changefreq><priority>0.3</priority></url><url><loc>https://example.com/hidden-path-but-in-sitemap/</loc></url></urlset>"')
}, 60000)
})

0 comments on commit 0674a18

Please sign in to comment.