15 lines
1.2 KiB
TypeScript
15 lines
1.2 KiB
TypeScript
import { readSiteDocument } from '../utils/db'
|
|
|
|
const escapeXml = (value: string) => value.replace(/[<>&'\"]/g, character => ({ '<': '<', '>': '>', '&': '&', "'": ''', '"': '"' })[character]!)
|
|
|
|
export default defineEventHandler((event) => {
|
|
const baseUrl = String(useRuntimeConfig().public.siteUrl).replace(/\/$/, '')
|
|
const { data, publishedAt } = readSiteDocument('published')
|
|
const paths = ['/', '/luhui', '/products', '/news', '/statements', '/team', '/privacy', '/terms']
|
|
data.products.filter(item => item.status === 'published').forEach(item => paths.push(`/products/${item.slug}`))
|
|
data.articles.filter(item => item.status === 'published').forEach(item => paths.push(`/${item.type === 'statement' ? 'statements' : 'news'}/${item.slug}`))
|
|
setHeader(event, 'Content-Type', 'application/xml; charset=utf-8')
|
|
setHeader(event, 'Cache-Control', 'public, max-age=300, stale-while-revalidate=600')
|
|
return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${paths.map(path => ` <url><loc>${escapeXml(`${baseUrl}${path}`)}</loc><lastmod>${escapeXml(publishedAt.slice(0, 10))}</lastmod></url>`).join('\n')}\n</urlset>`
|
|
})
|