feat: establish public SEO foundation
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import type { APIRoute } from "astro";
|
||||
import { api, type Activity } from "../lib/api";
|
||||
|
||||
const escapeXml = (value: string) => value.replace(/[<>&'\"]/g, character => ({ "<": "<", ">": ">", "&": "&", "'": "'", '"': """ })[character] ?? character);
|
||||
|
||||
export const GET: APIRoute = async ({ site }) => {
|
||||
const origin = site?.origin ?? import.meta.env.PUBLIC_SITE_URL ?? "https://rf4spotter.ru";
|
||||
const paths = new Set(["/", "/records", "/report", "/rules", "/privacy"]);
|
||||
try {
|
||||
const activity = await api<Activity[]>("/api/v1/activity?hours=72&limit=100");
|
||||
activity.forEach(item => paths.add(`/spots/${item.spot_id}`));
|
||||
} catch {
|
||||
// A temporary API outage must not make the static part of the sitemap unavailable.
|
||||
}
|
||||
const urls = [...paths].map(path => `<url><loc>${escapeXml(new URL(path, origin).toString())}</loc></url>`).join("");
|
||||
return new Response(`<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${urls}</urlset>`, {
|
||||
headers: { "Content-Type": "application/xml; charset=utf-8", "Cache-Control": "public, max-age=1800" },
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user