/** * `/sitemap.xml`, from the API's `/slugs` — one request for every published * address, instead of paging through each collection. * * Cached like any page: a record published a minute ago can wait a minute to * be listed. If the API is down the answer is a 503 rather than a sitemap * with half the site missing from it, which a crawler would believe. */ import { defineEventHandler, getRequestHeaders, setResponseHeader, setResponseStatus, } from 'h3'; import { pireonApi } from '../../app/core/api'; import { originFromHeaders } from '../../app/core/origin'; import { buildSitemap } from '../../app/core/sitemap'; export default defineEventHandler(async (event) => { const origin = originFromHeaders(getRequestHeaders(event)) ?? ''; const { data } = await pireonApi.slugs(); if (!data) { setResponseStatus(event, 503); setResponseHeader(event, 'Cache-Control', 'no-store'); return 'Sitemap temporarily unavailable'; } setResponseHeader(event, 'Content-Type', 'application/xml; charset=utf-8'); return buildSitemap(origin, data); });