import type { MetadataRoute } from "next"; import { API_URL } from "@/lib/api"; import { SITE_URL } from "@/lib/site"; /** * Segmented sitemap: static pages, exchanges, countries, then quoted instruments per asset class * (thin pages without a canonical quote are not listed). */ export async function generateSitemaps() { return [{ id: "static" }, { id: "exchanges" }, { id: "countries" }, { id: "stocks" }, { id: "etfs" }, { id: "indices" }, { id: "crypto" }, { id: "forex" }, { id: "rates" }, { id: "commodities" }]; } const CLASS: Record = { stocks: ["EQUITY"], etfs: ["ETF", "ETN"], indices: ["INDEX"], crypto: ["CRYPTO"], forex: ["FOREX"], rates: ["TREASURY", "BOND", "INTEREST_RATE"], commodities: ["COMMODITY", "FUTURE"] }; async function get(path: string): Promise { try { const r = await fetch(`${API_URL}${path}`, { cache: "no-store" }); if (!r.ok) return null; return ((await r.json()) as { data: T }).data; } catch { return null; } } export default async function sitemap({ id: rawId }: { id: string | Promise }): Promise { const id = String(await rawId); // Next 16 passes the segment param as a promise const now = new Date(); if (id === "static") { return ["", "/markets", "/live", "/stocks", "/etfs", "/indices", "/crypto", "/forex", "/rates", "/commodities", "/exchanges", "/countries", "/events", "/halts", "/filings", "/sources", "/coverage", "/connectors", "/data-health", "/status", "/compare", "/methodology", "/developers", "/licensing"].map((p) => ({ url: `${SITE_URL}${p}`, lastModified: now, changeFrequency: p === "" || p === "/live" ? "always" : "hourly", priority: p === "" ? 1 : 0.7 })); } if (id === "exchanges") { const xs = (await get>("/v1/exchanges")) ?? []; return xs.map((x) => ({ url: `${SITE_URL}/exchanges/${x.id}`, lastModified: now, changeFrequency: "hourly", priority: 0.6 })); } if (id === "countries") { const cs = (await get>("/v1/countries")) ?? []; return cs.map((c) => ({ url: `${SITE_URL}/countries/${c.code}`, lastModified: now, changeFrequency: "daily", priority: 0.5 })); } const classes = CLASS[id] ?? []; const out: MetadataRoute.Sitemap = []; for (const c of classes) { const rows = (await get>(`/v1/instruments?asset_class=${c}"ed=1&limit=500`)) ?? []; out.push(...rows.map((r) => ({ url: `${SITE_URL}/instruments/${encodeURIComponent(r.id)}`, lastModified: now, changeFrequency: "hourly" as const, priority: 0.6 }))); } return out; }