feat: generate /fr/sitemap.xml via custom Nitro route
@nuxtjs/sitemap is incompatible with app.baseURL: '/fr/' here — it broke nginx routing (403) on this deployment where /fr/ is served via an alias to a separate static build. A hand-rolled Nitro route sidesteps the module entirely and gets prerendered to a plain sitemap.xml file at the root of the generated output, matching what the nginx alias expects.
This commit is contained in:
@@ -44,5 +44,11 @@ export default defineNuxtConfig({
|
||||
}
|
||||
},
|
||||
|
||||
nitro: {
|
||||
prerender: {
|
||||
routes: ['/sitemap.xml']
|
||||
}
|
||||
},
|
||||
|
||||
compatibilityDate: '2024-10-24'
|
||||
})
|
||||
@@ -0,0 +1,23 @@
|
||||
import { serverQueryContent } from '#content/server'
|
||||
|
||||
// generates /fr/sitemap.xml without @nuxtjs/sitemap (incompatible with baseURL /fr/)
|
||||
export default defineEventHandler(async (event) => {
|
||||
const docs = await serverQueryContent(event).find()
|
||||
|
||||
const urls = docs
|
||||
.filter((doc: any) =>
|
||||
doc._path &&
|
||||
!doc._path.startsWith('/_') &&
|
||||
!doc._path.endsWith('/_dir')
|
||||
)
|
||||
.map((doc: any) => ` <url><loc>https://docu.djeex.fr/fr${doc._path}</loc></url>`)
|
||||
.join('\n')
|
||||
|
||||
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
${urls}
|
||||
</urlset>`
|
||||
|
||||
setHeader(event, 'Content-Type', 'application/xml')
|
||||
return sitemap
|
||||
})
|
||||
Reference in New Issue
Block a user