feat: 初始化慧遇书院官网一期
This commit is contained in:
6
server/routes/robots.txt.get.ts
Normal file
6
server/routes/robots.txt.get.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export default defineEventHandler((event) => {
|
||||
const baseUrl = String(useRuntimeConfig().public.siteUrl).replace(/\/$/, '')
|
||||
setHeader(event, 'Content-Type', 'text/plain; charset=utf-8')
|
||||
setHeader(event, 'Cache-Control', 'public, max-age=3600')
|
||||
return `User-agent: *\nAllow: /\nDisallow: /admin\nDisallow: /api/admin\nDisallow: /api/auth\n\nSitemap: ${baseUrl}/sitemap.xml\n`
|
||||
})
|
||||
14
server/routes/sitemap.xml.get.ts
Normal file
14
server/routes/sitemap.xml.get.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
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>`
|
||||
})
|
||||
Reference in New Issue
Block a user