Files
supabase/apps/docs/lib/last-changed.utils.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

49 lines
1.4 KiB
TypeScript

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<Database>,
signal: AbortSignal
): Promise<Map<string, string>> {
const dates = new Map<string, string>()
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
}