import type { MetadataRoute } from "next"; import { dbAvailable, getDb } from "@/lib/db"; export const dynamic = "force-dynamic"; export default function sitemap(): MetadataRoute.Sitemap { const base = "https://www.toveli.ca"; const now = new Date(); const fixed: MetadataRoute.Sitemap = ["", "/recherche?marche=achat", "/recherche?marche=location", "/carte", "/estimer", "/marche", "/financement", "/methodologie", "/assistant"].map((p) => ({ url: base + p, lastModified: now, changeFrequency: "daily", priority: p === "" ? 1 : 0.7 })); if (!dbAvailable()) return fixed; const cities = getDb().prepare("SELECT name, n_sale, n_rent FROM place WHERE kind='city' AND (n_sale >= 20 OR n_rent >= 20)").all() as { name: string; n_sale: number; n_rent: number }[]; const cityUrls: MetadataRoute.Sitemap = cities.flatMap((c) => [...(c.n_sale >= 20 ? [{ url: `${base}/recherche?marche=achat&ville=${encodeURIComponent(c.name)}`, lastModified: now, changeFrequency: "daily" as const, priority: 0.6 }] : []), ...(c.n_rent >= 20 ? [{ url: `${base}/recherche?marche=location&ville=${encodeURIComponent(c.name)}`, lastModified: now, changeFrequency: "daily" as const, priority: 0.6 }] : [])]); const listings = getDb().prepare("SELECT uid, last_seen FROM listing WHERE image_count > 0 AND quality_score >= 70 ORDER BY first_seen DESC LIMIT 20000").all() as { uid: string; last_seen: string | null }[]; return [...fixed, ...cityUrls, ...listings.map((l) => ({ url: `${base}/annonce/${encodeURIComponent(l.uid)}`, lastModified: l.last_seen ? new Date(l.last_seen) : now, changeFrequency: "weekly" as const, priority: 0.5 }))]; }