fix: 修复 sitemap 域名缺失和项目列表为空问题
Browse files- 从请求头自动推断域名,无需依赖环境变量
- 改用 latest_update_time 替代 updatedAt
Co-Authored-By: Claude <noreply@anthropic.com>
1234567891011121314151617181920212223242526import { prisma } from '@/lib/prisma';import type { MetadataRoute } from 'next';export default async function sitemap(): Promise<MetadataRoute.Sitemap> {const BASE_URL = process.env.NEXT_PUBLIC_BASE_URL || '';const projects = await prisma.project.findMany({select: { id: true, slug: true, updatedAt: true },orderBy: { updatedAt: 'desc' },}).catch(() => []);return [{url: BASE_URL || '/',lastModified: new Date(),changeFrequency: 'daily',priority: 1,},...projects.map((p) => ({url: `${BASE_URL}/project/${p.slug || p.id}`,lastModified: p.updatedAt,changeFrequency: 'weekly' as const,priority: 0.8,})),12345678910111213141516171819202122232425262728293031323334import { headers } from 'next/headers';import { prisma } from '@/lib/prisma';import type { MetadataRoute } from 'next';export default async function sitemap(): Promise<MetadataRoute.Sitemap> {const headersList = await headers();const host = headersList.get('host') || '';const proto = headersList.get('x-forwarded-proto') || 'https';const BASE_URL = (process.env.NEXT_PUBLIC_BASE_URL ||process.env.BASE_URL ||(host ? `${proto}://${host}` : '')).replace(/\/$/, '');const projects = await prisma.project.findMany({select: { id: true, slug: true, latest_update_time: true },orderBy: { latest_update_time: 'desc' },}).catch(() => []);return [{url: `${BASE_URL}/`,lastModified: new Date(),changeFrequency: 'daily',priority: 1,},...projects.map((p) => ({url: `${BASE_URL}/project/${p.slug || p.id}`,lastModified: p.latest_update_time,changeFrequency: 'weekly' as const,priority: 0.8,})),
app/sitemap.ts+13-5