N
Nuxt2mo ago
Blouflash

Empty nuxt-content-urls.json

I am using nuxt/content together with nuxt/sitemap to generate a xml file with all routes. However the content routes are not included in the sitemap s I debugged the sitemap and found out that the endpoint to fetch the sitemap for the content directory is returning an empty array even tho I have content pages.
5 Replies
kapa.ai
kapa.ai2mo ago
Hi @Blouflash. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To address your issue with nuxt/content and nuxt/sitemap where content routes are not included in the sitemap, there are a few potential solutions: 1. Ensure you're using the correct configuration for the sitemap module. According to the Nuxt Content documentation (https://content.nuxt.com/v1/community/integrations/#nuxtjssitemap), you should declare the sitemap module last in your nuxt.config.js file and provide a configuration object:
export default {
modules: [
'@nuxt/content',
'@nuxtjs/sitemap'
],
sitemap: {
hostname: '<https://my-domain-name.com',>
gzip: true,
routes: createSitemapRoutes
},
}
export default {
modules: [
'@nuxt/content',
'@nuxtjs/sitemap'
],
sitemap: {
hostname: '<https://my-domain-name.com',>
gzip: true,
routes: createSitemapRoutes
},
}
2. Implement a createSitemapRoutes function to dynamically generate routes for your content:
const createSitemapRoutes = async () => {
let routes = [];
const { $content } = require('@nuxt/content')
const posts = await $content('blog').fetch();
for (const post of posts) {
routes.push(`blog/${post.slug}`);
}
return routes;
}
const createSitemapRoutes = async () => {
let routes = [];
const { $content } = require('@nuxt/content')
const posts = await $content('blog').fetch();
for (const post of posts) {
routes.push(`blog/${post.slug}`);
}
return routes;
}
3. If you're using Nuxt 3, you might need to use a different approach. As mentioned in a Stack Overflow post (https://stackoverflow.com/questions/78008815), you may need to add an API source for nuxt sitemap to generate the pages. For dynamic pages, refer to the NuxtSEO documentation on dynamic URLs (https://nuxtseo.com/sitemap/guides/dynamic-urls). 4. Another approach is to create a server route to generate the sitemap. The Nuxt Content documentation (https://content.nuxt.com/recipes/sitemap/#server-route) provides an example of how to do this:
// server/routes/sitemap.xml.ts
import { serverQueryContent } from '#content/server'
import { SitemapStream, streamToPromise } from 'sitemap'
export default defineEventHandler(async (event) => {
const docs = await serverQueryContent(event).find()
const sitemap = new SitemapStream({
hostname: '<https://example.com'>
})

for (const doc of docs) {
sitemap.write({
url: doc._path,
changefreq: 'monthly'
})
}
sitemap.end()

return streamToPromise(sitemap)
})
// server/routes/sitemap.xml.ts
import { serverQueryContent } from '#content/server'
import { SitemapStream, streamToPromise } from 'sitemap'
export default defineEventHandler(async (event) => {
const docs = await serverQueryContent(event).find()
const sitemap = new SitemapStream({
hostname: '<https://example.com'>
})

for (const doc of docs) {
sitemap.write({
url: doc._path,
changefreq: 'monthly'
})
}
sitemap.end()

return streamToPromise(sitemap)
})
5. If you're using nuxt generate, you may need to pre-render the sitemap. Add this to your nuxt.config.ts:
export default defineNuxtConfig({
nitro: {
prerender: {
routes: ['/sitemap.xml']
}
}
})
export default defineNuxtConfig({
nitro: {
prerender: {
routes: ['/sitemap.xml']
}
}
})
If these solutions don't resolve your issue, you may need to debug further to ensure that your content is being properly fetched and that there are no errors in your configuration or content structure. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@78895484986458112> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Blouflash
BlouflashOP2mo ago
I have this enabled:
export default defineNuxtConfig({
sitemap: {
strictNuxtContentPaths: true
}
})
export default defineNuxtConfig({
sitemap: {
strictNuxtContentPaths: true
}
})
Blouflash
BlouflashOP2mo ago
and when debugging i also see that it tries to fetch from the integrated api from nux/content
No description
Blouflash
BlouflashOP2mo ago
it works in a production build
Want results from more Discord servers?
Add your server