| 1 | import { OG_RENDER_VERSION } from "@g1t/contracts/og"; |
| 2 | |
| 3 | /** |
| 4 | * The version of the cards' design, part of every cache key. Bump it (in |
| 5 | * `packages/contracts/src/og.ts`, which the site and the docs read too) |
| 6 | * whenever the card design changes, so the cards already cached are drawn |
| 7 | * again and every page links to a new address. |
| 8 | */ |
| 9 | export const RENDER_VERSION = OG_RENDER_VERSION; |
| 10 | |
| 11 | /** |
| 12 | * The address a card is cached under: the render version, then only the |
| 13 | * parameters that change the card, in order, including the page's own |
| 14 | * content version `v`, so stray ones (trackers, cache busters) cannot fill |
| 15 | * the cache. |
| 16 | */ |
| 17 | export function cacheKey(url: URL): string { |
| 18 | const wanted = |
| 19 | url.pathname === "/docs" ? ["title", "section", "description", "v"] : url.pathname === "/image" ? ["path", "v"] : []; |
| 20 | const key = new URL(url.pathname, url.origin); |
| 21 | key.searchParams.set("render", RENDER_VERSION); |
| 22 | for (const name of wanted) { |
| 23 | const value = url.searchParams.get(name); |
| 24 | if (value !== null) key.searchParams.set(name, value); |
| 25 | } |
| 26 | return key.toString(); |
| 27 | } |