Skip to content

Commit

Permalink
feat: appendSitemaps config for improved type safety
Browse files Browse the repository at this point in the history
Fixes #222
  • Loading branch information
harlan-zw committed Feb 1, 2024
1 parent 225da13 commit 609caf1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/module.ts
Expand Up @@ -212,8 +212,9 @@ export default defineNuxtModule<ModuleOptions>({
// if they haven't set `sitemaps` explicitly then we can set it up automatically for them
if (canI18nMap && resolvedAutoI18n) {
// @ts-expect-error untyped
config.sitemaps = { index: config.sitemaps?.index || [] }
config.sitemaps = { index: [...(config.sitemaps?.index || []), ...(config.appendSitemaps || [])] }
for (const locale of resolvedAutoI18n.locales)
// @ts-expect-error untyped
config.sitemaps[locale.iso || locale.code] = { includeAppSources: true }
isI18nMapped = true
usingMultiSitemaps = true
Expand Down Expand Up @@ -379,9 +380,8 @@ declare module 'vue-router' {
sitemaps.index = {
sitemapName: 'index',
_route: withBase('sitemap_index.xml', nuxt.options.app.baseURL || '/'),
// TODO better index support
// @ts-expect-error untyped
sitemaps: config.sitemaps!.index || [],
sitemaps: [...(config.sitemaps!.index || []), ...(config.appendSitemaps || [])],
}
if (typeof config.sitemaps === 'object') {
for (const sitemapName in config.sitemaps) {
Expand Down
7 changes: 5 additions & 2 deletions src/runtime/nitro/sitemap/builder/sitemap-index.ts
Expand Up @@ -103,8 +103,11 @@ export async function buildSitemapIndex(resolvers: NitroUrlResolvers, runtimeCon
}

// allow extending the index sitemap
if (sitemaps.index)
entries.push(...sitemaps.index.sitemaps)
if (sitemaps.index) {
entries.push(...sitemaps.index.sitemaps.map((entry) => {
return typeof entry === 'string' ? { sitemap: entry } : entry
}))
}

const sitemapXml = entries.map(e => [
' <sitemap>',
Expand Down
6 changes: 6 additions & 0 deletions src/runtime/types.ts
Expand Up @@ -43,6 +43,12 @@ export interface ModuleOptions extends SitemapDefinition {
* @default false
*/
sitemaps?: boolean | MultiSitemapsInput
/**
* Sitemaps to append to the sitemap index.
*
* This will only do anything when using multiple sitemaps.
*/
appendSitemaps?: (string | SitemapIndexEntry)[]
/**
* Path to the xsl that styles sitemap.xml.
*
Expand Down

0 comments on commit 609caf1

Please sign in to comment.