import 'server-only' import * as Sentry from '@sentry/nextjs' import { fetchGuidePageDates } from '~/lib/last-changed.utils' import { supabaseAdmin } from '~/lib/supabaseAdmin' /** * Upper bound on the whole paginated read. Guide pages wait on this lookup, so * a stalled database must fall back to rendering without a date instead of * holding up static generation. */ const LOOKUP_TIMEOUT_MS = 30_000 /** * After a failed lookup, skip new lookups for this long. Otherwise every guide * render during an outage would start a new scan and wait out another timeout. */ const FAILURE_COOLDOWN_MS = 60_000 /** * Builds map of all pages and then reads from this, so the table is read once per worker * rather than once per page. A failed load is not cached: it starts a cooldown, and * the first call after it retries. */ let pageDates: Promise> | undefined let cooldownUntil = 0 function loadPageDates() { if (Date.now() < cooldownUntil) { return Promise.resolve(new Map()) } if (!pageDates) { // Async wrapper so a synchronous throw from `supabaseAdmin()` (missing env) // lands in the catch below instead of failing the page render. pageDates = (async () => fetchGuidePageDates(supabaseAdmin(), AbortSignal.timeout(LOOKUP_TIMEOUT_MS)))().catch( (error) => { Sentry.captureException(error) pageDates = undefined cooldownUntil = Date.now() + FAILURE_COOLDOWN_MS return new Map() } ) } return pageDates } /** * Returns the most recent content change for a guide page, as recorded in the * `last_changed` table by `scripts/last-changed.ts`. * * Returns `undefined` when the page has no rows or the table could not be * read, so the page renders without a date rather than failing. */ export async function getGuideLastUpdated(slug: string[]): Promise { const dates = await loadPageDates() return dates.get(`/content/guides/${slug.join('/')}.mdx`) }