diff --git a/docs/content/2.guides/0.multi-sitemaps.md b/docs/content/2.guides/0.multi-sitemaps.md index f5d5422b..84e1d823 100644 --- a/docs/content/2.guides/0.multi-sitemaps.md +++ b/docs/content/2.guides/0.multi-sitemaps.md @@ -60,7 +60,7 @@ combined with changing the sitemap key to what you'd like the name to be. ```ts export default defineNuxtConfig({ sitemap: { - sitemapsPathPrefix: '/', + sitemapsPathPrefix: '/', // or false sitemaps: { // will be available at /sitemap-foo.xml ['sitemap-foo']: { diff --git a/docs/content/4.api/0.config.md b/docs/content/4.api/0.config.md index 323ba3a8..aefcf951 100644 --- a/docs/content/4.api/0.config.md +++ b/docs/content/4.api/0.config.md @@ -169,6 +169,13 @@ without documentDriven mode. The time in seconds to cache the sitemaps. +## `sitemapsPathPrefix` + +- Type: `string | false` +- Default: `/__sitemap__/` + +The path prefix for the sitemaps when using multiple sitemaps. + ## `runtimeCacheStorage` - Type: `boolean | (Record & { driver: string })` diff --git a/src/module.ts b/src/module.ts index e2ad3572..3bc8a6e8 100644 --- a/src/module.ts +++ b/src/module.ts @@ -338,7 +338,7 @@ declare module 'vue-router' { nuxt.options.nitro.routeRules['/sitemap_index.xml'] = routeRules if (typeof config.sitemaps === 'object') { for (const k in config.sitemaps) { - nuxt.options.nitro.routeRules[joinURL(config.sitemapsPathPrefix, `/${k}.xml`)] = routeRules + nuxt.options.nitro.routeRules[joinURL(config.sitemapsPathPrefix || '', `/${k}.xml`)] = routeRules } } else { @@ -475,12 +475,25 @@ declare module 'vue-router' { lazy: true, middleware: false, }) - addServerHandler({ - route: joinURL(config.sitemapsPathPrefix, `/**:sitemap`), - handler: resolve('./runtime/server/routes/sitemap/[sitemap].xml'), - lazy: true, - middleware: false, - }) + if (config.sitemapsPathPrefix && config.sitemapsPathPrefix !== '/') { + addServerHandler({ + route: joinURL(config.sitemapsPathPrefix, `/**:sitemap`), + handler: resolve('./runtime/server/routes/sitemap/[sitemap].xml'), + lazy: true, + middleware: false, + }) + } + else { + // register each key as a route + for (const sitemapName of Object.keys(config.sitemaps || {})) { + addServerHandler({ + route: withLeadingSlash(`${sitemapName}.xml`), + handler: resolve('./runtime/server/routes/sitemap/[sitemap].xml'), + lazy: true, + middleware: false, + }) + } + } sitemaps.index = { sitemapName: 'index', _route: withBase('sitemap_index.xml', nuxt.options.app.baseURL || '/'), @@ -495,7 +508,7 @@ declare module 'vue-router' { sitemaps[sitemapName as keyof typeof sitemaps] = defu( { sitemapName, - _route: withBase(joinURL(config.sitemapsPathPrefix, `${sitemapName}.xml`), nuxt.options.app.baseURL || '/'), + _route: withBase(joinURL(config.sitemapsPathPrefix || '', `${sitemapName}.xml`), nuxt.options.app.baseURL || '/'), _hasSourceChunk: typeof definition.urls !== 'undefined' || definition.sources?.length, }, { ...definition, urls: undefined, sources: undefined }, diff --git a/src/runtime/server/routes/sitemap/[sitemap].xml.ts b/src/runtime/server/routes/sitemap/[sitemap].xml.ts index ca10b2e4..76e6eee9 100644 --- a/src/runtime/server/routes/sitemap/[sitemap].xml.ts +++ b/src/runtime/server/routes/sitemap/[sitemap].xml.ts @@ -8,7 +8,7 @@ export default defineEventHandler(async (e) => { const { sitemaps } = runtimeConfig const sitemapName = withoutLeadingSlash(withoutTrailingSlash((getRouterParam(e, 'sitemap') || e.path)?.replace('.xml', '') - .replace(runtimeConfig.sitemapsPathPrefix, ''))) + .replace(runtimeConfig.sitemapsPathPrefix || '', ''))) // check if sitemapName can be cast to a number safely const isChunking = typeof sitemaps.chunks !== 'undefined' && !Number.isNaN(Number(sitemapName)) if (!sitemapName || (!(sitemapName in sitemaps) && !isChunking)) { diff --git a/src/runtime/server/routes/sitemap_index.xml.ts b/src/runtime/server/routes/sitemap_index.xml.ts index 1027f1f5..d19e13a5 100644 --- a/src/runtime/server/routes/sitemap_index.xml.ts +++ b/src/runtime/server/routes/sitemap_index.xml.ts @@ -19,7 +19,7 @@ export default defineEventHandler(async (e) => { e, 'x-nitro-prerender', sitemaps.filter(entry => !!entry._sitemapName) - .map(entry => encodeURIComponent(joinURL(runtimeConfig.sitemapsPathPrefix, `/${entry._sitemapName}.xml`))).join(', '), + .map(entry => encodeURIComponent(joinURL(runtimeConfig.sitemapsPathPrefix || '', `/${entry._sitemapName}.xml`))).join(', '), ) } diff --git a/src/runtime/server/sitemap/builder/sitemap-index.ts b/src/runtime/server/sitemap/builder/sitemap-index.ts index 8c2fdeee..3359ce47 100644 --- a/src/runtime/server/sitemap/builder/sitemap-index.ts +++ b/src/runtime/server/sitemap/builder/sitemap-index.ts @@ -72,7 +72,7 @@ export async function buildSitemapIndex(resolvers: NitroUrlResolvers, runtimeCon const sitemap = chunks[name] const entry: SitemapIndexEntry = { _sitemapName: name, - sitemap: resolvers.canonicalUrlResolver(joinURL(sitemapsPathPrefix, `/${name}.xml`)), + sitemap: resolvers.canonicalUrlResolver(joinURL(sitemapsPathPrefix || '', `/${name}.xml`)), } let lastmod = sitemap.urls .filter(a => !!a?.lastmod) diff --git a/src/runtime/types.ts b/src/runtime/types.ts index 110fc4b4..591c1f29 100644 --- a/src/runtime/types.ts +++ b/src/runtime/types.ts @@ -49,7 +49,7 @@ export interface ModuleOptions extends SitemapDefinition { * * @default /__sitemap__/ */ - sitemapsPathPrefix: string + sitemapsPathPrefix: string | false /** * Sitemaps to append to the sitemap index. *