Skip to content

Commit

Permalink
fix: nested dynamicUrlsApiEndpoint calls
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Jul 7, 2023
1 parent 08e72dd commit 4304ccf
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
10 changes: 10 additions & 0 deletions .playground/server/api/multi-sitemap-sources/bar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineEventHandler } from 'h3'

export default defineEventHandler(e => {
const posts = Array.from({ length: 5 }, (_, i) => i + 1)
return [
...posts.map(post => ({
loc: `/bar/${post}`
}))
]
})
10 changes: 10 additions & 0 deletions .playground/server/api/multi-sitemap-sources/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineEventHandler } from 'h3'

export default defineEventHandler(e => {
const posts = Array.from({ length: 5 }, (_, i) => i + 1)
return [
...posts.map(post => ({
loc: `/foo/${post}`
}))
]
})
7 changes: 4 additions & 3 deletions src/runtime/sitemap/entries/asyncData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ export async function resolveAsyncSitemapData(input: BuildSitemapInput | BuildSi
)
}

if (input.sitemap?.dynamicUrlsApiEndpoint) {
const customEndpoint = input.sitemap?.dynamicUrlsApiEndpoint || input.moduleConfig.dynamicUrlsApiEndpoint
if (customEndpoint) {
waitables.push(
// should just work
globalThis.$fetch(input.moduleConfig.dynamicUrlsApiEndpoint, {
globalThis.$fetch(customEndpoint, {
responseType: 'json',
}).then((urls) => {
entries.push({
context: `sitemap:${input.sitemap!.sitemapName}:${input.moduleConfig.dynamicUrlsApiEndpoint.replaceAll('/', '.')}`,
context: `sitemap:${input.sitemap!.sitemapName}:source:${customEndpoint.replaceAll('/', '.')}`,
urls: urls as SitemapEntry[],
})
}),
Expand Down
61 changes: 61 additions & 0 deletions test/multiEndpoints.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { describe, expect, it } from 'vitest'
import { createResolver } from '@nuxt/kit'
import { $fetch, setup } from '@nuxt/test-utils'

const { resolve } = createResolver(import.meta.url)

await setup({
rootDir: resolve('../.playground'),
build: true,
server: true,
nuxtConfig: {
sitemap: {
credits: false,
sitemaps: {
foo: {
include: ['/foo/*'],
dynamicUrlsApiEndpoint: '/api/multi-sitemap-sources/foo',
},
bar: {
include: ['/bar/*'],
dynamicUrlsApiEndpoint: '/api/multi-sitemap-sources/bar',
},
},
sitemapName: 'test.xml',
siteUrl: 'https://nuxtseo.com',
},
},
})
describe('multiEndpoints', () => {
it('basic', async () => {
let sitemap = await $fetch('/foo-sitemap.xml')
// remove lastmods before tresting
sitemap = sitemap.replace(/lastmod>(.*?)</g, 'lastmod><')
// basic test to make sure we get a valid response
expect(sitemap).toMatchInlineSnapshot(`
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><?xml-stylesheet type=\\"text/xsl\\" href=\\"/__sitemap__/style.xsl\\"?>
<urlset xmlns:xsi=\\"http://www.w3.org/2001/XMLSchema-instance\\" xmlns:video=\\"http://www.google.com/schemas/sitemap-video/1.1\\" xmlns:xhtml=\\"http://www.w3.org/1999/xhtml\\" xmlns:image=\\"http://www.google.com/schemas/sitemap-image/1.1\\" xsi:schemaLocation=\\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd\\" xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\">
<url>
<lastmod></lastmod>
<loc>https://nuxtseo.com/foo/1</loc>
</url>
<url>
<lastmod></lastmod>
<loc>https://nuxtseo.com/foo/2</loc>
</url>
<url>
<lastmod></lastmod>
<loc>https://nuxtseo.com/foo/3</loc>
</url>
<url>
<lastmod></lastmod>
<loc>https://nuxtseo.com/foo/4</loc>
</url>
<url>
<lastmod></lastmod>
<loc>https://nuxtseo.com/foo/5</loc>
</url>
</urlset>"
`)
}, 60000)
})

0 comments on commit 4304ccf

Please sign in to comment.