Nuxt3 Sitemap Generation

1.2k Views Asked by At

I'm generating the sitemap within a site with dynamic routes. I'm using nuxt version 3. According to the documentation provided here, to create the sitemap in a site with dynamic routes it is necessary to create the folder /server/routes/sitemap.xml.ts file.

Inside the file i put this code it seems according to the documentation

import { serverQueryContent } from '#content/server'
import { SitemapStream, streamToPromise } from 'sitemap'

export default defineEventHandler(async (event) => {
// Fetch all documents
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)
})

This error is returned

[worker reload] [worker init] Package import specifier "#content/server" is not 
defined in package /Users/myuser/Desktop/websites/mysite/nuxt- 
app/package.json imported from /Users/myuser/Desktop/websites/mysite.it/nuxt- 
app/.nuxt/dev/index.mjs

I can't find anything about it

1

There are 1 best solutions below

0
dakota On

you need to install nuxt content module.

Here is the link

https://content.nuxtjs.org/get-started

  1. npm install --save-dev @nuxt/content
  2. nuxt.config.ts modules: [ '@nuxt/content' ],
  3. create a directory called 'content' in the root of the project.
  4. Then, create markdown files inside content directory for all of your routes. for example , for '/' route, create index.md
  5. It should be working fine.