Skip to content

Commit c7b1ebd

Browse files
committedJan 17, 2025·
fix: respect falsy sitemapsPathPrefix
Fixes #368
1 parent 579f70e commit c7b1ebd

File tree

7 files changed

+33
-13
lines changed

7 files changed

+33
-13
lines changed
 

Diff for: ‎docs/content/2.guides/0.multi-sitemaps.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ combined with changing the sitemap key to what you'd like the name to be.
6060
```ts
6161
export default defineNuxtConfig({
6262
sitemap: {
63-
sitemapsPathPrefix: '/',
63+
sitemapsPathPrefix: '/', // or false
6464
sitemaps: {
6565
// will be available at /sitemap-foo.xml
6666
['sitemap-foo']: {

Diff for: ‎docs/content/4.api/0.config.md

+7
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ without documentDriven mode.
169169

170170
The time in seconds to cache the sitemaps.
171171

172+
## `sitemapsPathPrefix`
173+
174+
- Type: `string | false`
175+
- Default: `/__sitemap__/`
176+
177+
The path prefix for the sitemaps when using multiple sitemaps.
178+
172179
## `runtimeCacheStorage`
173180

174181
- Type: `boolean | (Record<string, any> & { driver: string })`

Diff for: ‎src/module.ts

+21-8
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ declare module 'vue-router' {
338338
nuxt.options.nitro.routeRules['/sitemap_index.xml'] = routeRules
339339
if (typeof config.sitemaps === 'object') {
340340
for (const k in config.sitemaps) {
341-
nuxt.options.nitro.routeRules[joinURL(config.sitemapsPathPrefix, `/${k}.xml`)] = routeRules
341+
nuxt.options.nitro.routeRules[joinURL(config.sitemapsPathPrefix || '', `/${k}.xml`)] = routeRules
342342
}
343343
}
344344
else {
@@ -475,12 +475,25 @@ declare module 'vue-router' {
475475
lazy: true,
476476
middleware: false,
477477
})
478-
addServerHandler({
479-
route: joinURL(config.sitemapsPathPrefix, `/**:sitemap`),
480-
handler: resolve('./runtime/server/routes/sitemap/[sitemap].xml'),
481-
lazy: true,
482-
middleware: false,
483-
})
478+
if (config.sitemapsPathPrefix && config.sitemapsPathPrefix !== '/') {
479+
addServerHandler({
480+
route: joinURL(config.sitemapsPathPrefix, `/**:sitemap`),
481+
handler: resolve('./runtime/server/routes/sitemap/[sitemap].xml'),
482+
lazy: true,
483+
middleware: false,
484+
})
485+
}
486+
else {
487+
// register each key as a route
488+
for (const sitemapName of Object.keys(config.sitemaps || {})) {
489+
addServerHandler({
490+
route: withLeadingSlash(`${sitemapName}.xml`),
491+
handler: resolve('./runtime/server/routes/sitemap/[sitemap].xml'),
492+
lazy: true,
493+
middleware: false,
494+
})
495+
}
496+
}
484497
sitemaps.index = {
485498
sitemapName: 'index',
486499
_route: withBase('sitemap_index.xml', nuxt.options.app.baseURL || '/'),
@@ -495,7 +508,7 @@ declare module 'vue-router' {
495508
sitemaps[sitemapName as keyof typeof sitemaps] = defu(
496509
{
497510
sitemapName,
498-
_route: withBase(joinURL(config.sitemapsPathPrefix, `${sitemapName}.xml`), nuxt.options.app.baseURL || '/'),
511+
_route: withBase(joinURL(config.sitemapsPathPrefix || '', `${sitemapName}.xml`), nuxt.options.app.baseURL || '/'),
499512
_hasSourceChunk: typeof definition.urls !== 'undefined' || definition.sources?.length,
500513
},
501514
{ ...definition, urls: undefined, sources: undefined },

Diff for: ‎src/runtime/server/routes/sitemap/[sitemap].xml.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default defineEventHandler(async (e) => {
88
const { sitemaps } = runtimeConfig
99

1010
const sitemapName = withoutLeadingSlash(withoutTrailingSlash((getRouterParam(e, 'sitemap') || e.path)?.replace('.xml', '')
11-
.replace(runtimeConfig.sitemapsPathPrefix, '')))
11+
.replace(runtimeConfig.sitemapsPathPrefix || '', '')))
1212
// check if sitemapName can be cast to a number safely
1313
const isChunking = typeof sitemaps.chunks !== 'undefined' && !Number.isNaN(Number(sitemapName))
1414
if (!sitemapName || (!(sitemapName in sitemaps) && !isChunking)) {

Diff for: ‎src/runtime/server/routes/sitemap_index.xml.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default defineEventHandler(async (e) => {
1919
e,
2020
'x-nitro-prerender',
2121
sitemaps.filter(entry => !!entry._sitemapName)
22-
.map(entry => encodeURIComponent(joinURL(runtimeConfig.sitemapsPathPrefix, `/${entry._sitemapName}.xml`))).join(', '),
22+
.map(entry => encodeURIComponent(joinURL(runtimeConfig.sitemapsPathPrefix || '', `/${entry._sitemapName}.xml`))).join(', '),
2323
)
2424
}
2525

Diff for: ‎src/runtime/server/sitemap/builder/sitemap-index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export async function buildSitemapIndex(resolvers: NitroUrlResolvers, runtimeCon
7272
const sitemap = chunks[name]
7373
const entry: SitemapIndexEntry = {
7474
_sitemapName: name,
75-
sitemap: resolvers.canonicalUrlResolver(joinURL(sitemapsPathPrefix, `/${name}.xml`)),
75+
sitemap: resolvers.canonicalUrlResolver(joinURL(sitemapsPathPrefix || '', `/${name}.xml`)),
7676
}
7777
let lastmod = sitemap.urls
7878
.filter(a => !!a?.lastmod)

Diff for: ‎src/runtime/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export interface ModuleOptions extends SitemapDefinition {
4949
*
5050
* @default /__sitemap__/
5151
*/
52-
sitemapsPathPrefix: string
52+
sitemapsPathPrefix: string | false
5353
/**
5454
* Sitemaps to append to the sitemap index.
5555
*

0 commit comments

Comments
 (0)
Please sign in to comment.