Skip to content

Commit

Permalink
fix: warn when providing non-serializable urls
Browse files Browse the repository at this point in the history
Fixes #285
  • Loading branch information
harlan-zw committed Jul 1, 2024
1 parent f2fc993 commit 8dfadc8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ declare module 'vue-router' {
name: 'sitemap:urls',
description: 'Set with the `sitemap.urls` config.',
},
urls: await resolveUrls(config.urls),
urls: await resolveUrls(config.urls, { path: 'sitemap:urls', logger }),
})
// we want to avoid adding duplicates as well as hitting api endpoints multiple times
resolvedConfigUrls = true
Expand Down Expand Up @@ -625,7 +625,7 @@ declare module 'vue-router' {
name: `sitemaps:${sitemapName}:urls`,
description: 'Set with the `sitemap.urls` config.',
},
urls: await resolveUrls(definition.urls),
urls: await resolveUrls(definition.urls, { path: `sitemaps:${sitemapName}:urls`, logger }),
})
definition!.dynamicUrlsApiEndpoint && sitemapSources[sitemapName].push({
context: {
Expand Down
12 changes: 11 additions & 1 deletion src/util/nuxtSitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@ import type { Nuxt } from '@nuxt/schema'
import { useNuxt } from '@nuxt/kit'
import { extname } from 'pathe'
import { defu } from 'defu'
import type { ConsolaInstance } from 'consola'
import type { SitemapDefinition, SitemapUrl, SitemapUrlInput } from '../runtime/types'
import { createPathFilter } from '../runtime/utils-pure'
import type { CreateFilterOptions } from '../runtime/utils-pure'

export async function resolveUrls(urls: Required<SitemapDefinition>['urls']): Promise<SitemapUrlInput[]> {
export async function resolveUrls(urls: Required<SitemapDefinition>['urls'], ctx: { logger: ConsolaInstance, path: string }): Promise<SitemapUrlInput[]> {
if (typeof urls === 'function')
urls = urls()
// resolve promise
urls = await urls
// we need to validate that the urls can be serialised properly for example to avoid circular references
try {
urls = JSON.parse(JSON.stringify(urls))
}
catch (e) {
ctx.logger.warn(`Failed to serialize ${typeof urls} \`${ctx.path}\`, please make sure that the urls resolve as a valid array without circular dependencies.`)
ctx.logger.error(e)
return []
}
return urls
}

Expand Down

0 comments on commit 8dfadc8

Please sign in to comment.