How to add blog post to sitemap.xml with Nuxt Content v3 ?
for nuxt content v2 i was uded something like this in server/routes:
But with Nuxt Content v3 struggeling to make it working
4 Replies
Hi @Ress. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To add blog posts to your sitemap.xml with Nuxt Content v3, you'll need to adjust your approach slightly from what you were using in v2. The main difference is that in v3, we use collections and a different query API. Here's how you can achieve this:
1. First, make sure you have defined your blog collection in
content.config.ts
:
2. Then, you can create a server route to generate your sitemap. Create a file server/routes/sitemap.xml.ts
:
3. Finally, make sure to add the sitemap to your prerender routes in your nuxt.config.ts
:
This approach uses the new serverQueryCollection
API from Nuxt Content v3 to fetch all blog posts and add them to the sitemap. The sitemap
package is used to generate the XML output.
Note that you'll need to install the sitemap
package:
This solution should work for Nuxt Content v3. Remember to adjust the hostname in the SitemapStream constructor to match your actual website URL.
Nuxt Content v3 Documentation provides more information about the new features and APIs in v3.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@328509260143001601> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
check out this: https://nuxt.com/modules/sitemap
Yes, it's working with 'asSitemapCollection' in content.conig.ts
Thanks!