Skip to content

Commit

Permalink
fix: respect falsy sitemapsPathPrefix
Browse files Browse the repository at this point in the history
Fixes #368
  • Loading branch information
harlan-zw committed Jan 17, 2025
1 parent 579f70e commit c7b1ebd
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/content/2.guides/0.multi-sitemaps.md
Original file line number Diff line number Diff line change
Expand Up @@ -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']: {
Expand Down
7 changes: 7 additions & 0 deletions docs/content/4.api/0.config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any> & { driver: string })`
Expand Down
29 changes: 21 additions & 8 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 || '/'),
Expand All @@ -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 },
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/server/routes/sitemap/[sitemap].xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/server/routes/sitemap_index.xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(', '),
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/server/sitemap/builder/sitemap-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface ModuleOptions extends SitemapDefinition {
*
* @default /__sitemap__/
*/
sitemapsPathPrefix: string
sitemapsPathPrefix: string | false
/**
* Sitemaps to append to the sitemap index.
*
Expand Down

0 comments on commit c7b1ebd

Please sign in to comment.