import { type SupabaseClient } from '@supabase/supabase-js' import { type Database } from '~/lib/supabase' /** * PostgREST returns at most 1,000 rows per request by default, so the table * is read in pages of this size. */ const PAGE_SIZE = 1000 /** * Reads every guide row from `last_changed` and reduces them to one date per * page: the newest `last_updated` across that page's section rows. * * Keys are content paths, for example `/content/guides/auth/jwts.mdx`. * * Kept free of `server-only` so build scripts such as the sitemap generator * can share it with the guide pages. */ export async function fetchGuidePageDates( client: SupabaseClient, signal: AbortSignal ): Promise> { const dates = new Map() for (let from = 0; ; from += PAGE_SIZE) { const { data, error } = await client .from('last_changed') .select('id, parent_page, last_updated') .like('parent_page', '/content/guides/%') .order('id', { ascending: true }) .abortSignal(signal) .range(from, from + PAGE_SIZE - 1) if (error) throw error for (const row of data) { const current = dates.get(row.parent_page) if (!current || Date.parse(row.last_updated) > Date.parse(current)) { dates.set(row.parent_page, row.last_updated) } } if (data.length < PAGE_SIZE) break } return dates }