Open History to see the last commit
1/**
2 * Sitemap + robots (PLAN.md task 10.1). Generated per request so they are
3 * always current on publish; origin comes from BETTER_AUTH_URL, lastmod from
4 * the newest change row.
5 */
6import { desc } from 'drizzle-orm'
7
8import type { Db } from '#/db/index.ts'
9import { changes } from '#/db/schema.ts'
10
11export const CANONICAL_PATHS = [
12 '/',
13 '/docs',
14 '/login',
15 '/account',
16 '/llms.txt',
17 '/openapi.json',
18 '/skill',
19] as const
20
21function escapeXml(value: string): string {
22 return value
23 .replaceAll('&', '&')
24 .replaceAll('<', '<')
25 .replaceAll('>', '>')
26}
27
28export async function sitemapXml(db: Db, origin: string): Promise<string> {
29 const latest = await db
30 .select({ createdAt: changes.createdAt })
31 .from(changes)
32 .orderBy(desc(changes.createdAt))
33 .limit(1)
34 const lastmod = latest[0]
35 ? new Date(latest[0].createdAt * 1000).toISOString().slice(0, 10)
36 : undefined
37
38 const urls = CANONICAL_PATHS.map((path) => {
39 const loc = escapeXml(`${origin}${path === '/' ? '' : path}` || origin)
40 return ` <url>\n <loc>${path === '/' ? escapeXml(origin + '/') : loc}</loc>${lastmod ? `\n <lastmod>${lastmod}</lastmod>` : ''}\n </url>`
41 }).join('\n')
42
43 return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${urls}\n</urlset>\n`
44}
45
46export function robotsTxt(origin: string): string {
47 return `User-agent: *\nAllow: /\n\n# agents: start at ${origin}/llms.txt\nSitemap: ${origin}/sitemap.xml\n`
48}