Files
supabase/apps/docs/lib/last-changed.ts
Miranda LimonczenkoandClaude 4d9726da1c feat(docs): add last-modified dates to the docs sitemap (#54329)
Closes DOCS-1753

## Problem

Guide pages already show a "Last updated" date
(supabase/supabase#52421), but `sitemap.xml` has no `<lastmod>`, so
crawlers get no recency signal.
## Solution

- Guide entries in `sitemap.xml` now include `<lastmod>`. The date comes
from the same `last_changed` table as the on-page date: the newest
`last_updated` across a page's sections, written as an ISO 8601
timestamp.
- The paginated `last_changed` read moved from `lib/last-changed.ts`
into `lib/last-changed.utils.ts`. The new module has no `server-only`
import, so the `tsx` sitemap script can share it. `getGuideLastUpdated`
behaves the same, including its timeout, cooldown, and Sentry reporting.
- When `NEXT_PUBLIC_SUPABASE_URL` or `SUPABASE_SECRET_KEY` is missing,
or the read fails, the script logs a warning and writes the sitemap
without `<lastmod>`. Local and preview builds still succeed.
- `build:sitemap` in `apps/docs/turbo.jsonc` is now uncached and passes
the two credentials through. The dates change nightly, outside the
task's inputs, so a cache hit could otherwise restore stale dates. The
task is cheap, and its dependencies stay cached.
- Reference pages don't get `<lastmod>` because `last_changed` only
tracks guides.
- `sitemap.md` (marked TBD in the issue) is out of scope.

## Review instructions

| Preview | Link |
| --- | --- |
| Docs sitemap |
https://docs-git-claude-compassionate-dirac-ns0vjr-supabase.vercel.app/docs/sitemap.xml
|
| Docs app |
https://docs-git-claude-compassionate-dirac-ns0vjr-supabase.vercel.app/docs
|

1. Check the `build:sitemap` step in the Vercel build log. It should
print `Total of N pages in sitemap (M with last-modified dates)`, where
M is greater than 0.
2. In the preview deploy, open `/docs/sitemap.xml` and confirm that
guide entries have `<lastmod>` values matching the "Last updated" date
on those pages.
3. Optional local check: run `pnpm -F docs build:sitemap` without
credentials. You should see the warning, and the sitemap should have no
`<lastmod>` entries.

Co-authored-by: Claude <noreply@anthropic.com>
2026-12-01 10:03:01 -08:00

62 lines
2.0 KiB
TypeScript

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<Map<string, string>> | undefined
let cooldownUntil = 0
function loadPageDates() {
if (Date.now() < cooldownUntil) {
return Promise.resolve(new Map<string, string>())
}
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<string, string>()
}
)
}
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<string | undefined> {
const dates = await loadPageDates()
return dates.get(`/content/guides/${slug.join('/')}.mdx`)
}