I'm working on a Nuxt 3 application where I need to generate a sitemap dynamically from a CMS using the method described for Nuxt Content. I'm using the built in /server/api/ directory to get this data for the rest of the app. My sitemap.xml.js file is in /server/routes/.
I'd like to be able to make $fetch requests from my sitemap file to the apis instead of rewriting all of that code (making the $fetch requests to the CMS again).
sitemap.xml.js
export default defineEventHandler(async (event) => {
const { auth, posts } = useRuntimeConfig()
//[...]
const { records: postList } = await $fetch(`${posts}?api_key=${auth}`)
postList.forEach((post) => {
if(post.fields.Status){
sitemap.write({
url: 'posts/' + post.fields.Slug + '/',
lastmod: post.lastMod
})
}
})
//[...]
}
What I would like to do
export default defineEventHandler(async (event) => {
//[...]
const { records: postList } = await $fetch('/api/posts')
postList.forEach((post) => {
sitemap.write({
url: 'posts/' + post.fields.Slug + '/',
lastmod: post.lastMod
})
})
//[...]
}
My sitemap code all works fine, I just can't get the data from my other server routes. Does anyone know if this is possible/how to do it?