Files
supabase/apps/docs/lib/last-changed.ts
03c2008987 feat(docs) - add last updated date (#52421)
## Problem

Per #52168, dates are a really important signal for LLMs. We should be
surfacing this more proactively across our docs, pulling from the
lastModified date.

## Solution

Looks like this information is already stored in a Supabase table, so we
can grab the date values and then render on associated pages.

To this end, this PR:
- Grabs the last-updated date with a script (with tests on the script).
Gets all the dates to form a map, and then have all the pages read from
that map to save roundtrips. Still ~20+ lookups b/c of how the site is
built (by multiple workers).
- Adds that date into the Guides template and associated JSON-LD /
opengraph structured data.

The goal of these dates -- to answer the various CodeRabbit comments --
are to be an **approximation** of age. It's okay if they're not 100%
accurate based on changed titles, frontmatter, deleted heading sections,
etc.

Reading this via JS instead of a SQL lookup to save us a DB migration,
as that's the fastest way to get this change up (and I have very little
familiarity with db migrations, so partially just lack of knowledge
here).

This does add some additional latency to builds, approximately ~20
seconds from some build benchmarking:
-
https://vercel.com/supabase/docs/7UcfoR3Jjb8SYuUNkRo14W3DZqBL#L1221-L1241
-
https://vercel.com/supabase/docs/23RH6uPtR92K8EggPPiTEQvY23bk#L1062-L1085

## Preview links

| state | preview |
| -------|------|
| after | <img width="891" height="438" alt="image"
src="https://github.com/user-attachments/assets/38476245-be80-4b65-99a7-4501e60560fc"
/> |

- [Queues
overview](https://docs-git-kody-devrel-751-docs-last-updated-date-supabase.vercel.app/docs/guides/queues)
-- rendered date of Oct 19th matches [git last
modified](https://github.com/supabase/supabase/commits/master/apps/docs/content/guides/queues.mdx)
- [Cron
overview](https://docs-git-kody-devrel-751-docs-last-updated-date-supabase.vercel.app/docs/guides/cron)
-- rendered date of Feb 23rd slight diff from git last modified... but
matches last [content
change](https://github.com/supabase/supabase/commits/master/apps/docs/content/guides/cron.mdx)
- [Auth
overview](https://docs-git-kody-devrel-751-docs-last-updated-date-supabase.vercel.app/docs/guides/auth)
-- rendered date of Oct 22nd matches [git last
modified](https://github.com/supabase/supabase/commits/master/apps/docs/content/guides/auth.mdx)

## Gaps / open review questions

- **testing** - Least familiar with the tests in
`apps/docs/lib/last-changed.test.ts` + these are entirely claude
generated. Unsure if they hit what they should.
- **build latency** - Is ~20 sec / build acceptable latency here? If we
wanted to move forward with SQL instead of JS to try and reduce it,
could make sense... but I'd definitely need help on the associated db
migration here.
- **build fragility** - AI review flagged the cooldown / potential
hanging requests without a timeout. Seems like this is now addressed,
but obviously could use a sanity check.

---------

Co-authored-by: Claude Haiku 5.5 <noreply@anthropic.com>
Co-authored-by: anthony lio <lionnet.ant@gmail.com>
2026-11-02 08:00:09 -06:00

93 lines
2.7 KiB
TypeScript

import 'server-only'
import * as Sentry from '@sentry/nextjs'
import { supabaseAdmin } from '~/lib/supabaseAdmin'
/**
* 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
/**
* 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
/**
* 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.
*/
async function fetchAllPageDates(signal: AbortSignal) {
const dates = new Map<string, string>()
for (let from = 0; ; from += PAGE_SIZE) {
const { data, error } = await supabaseAdmin()
.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
}
/**
* 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) {
pageDates = fetchAllPageDates(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`)
}