flagon-io/g1t

public

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t/services/og/src/cache.ts

27 lines1,076 bytesCodeBlame
1import { 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 */
9export 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 */
17export 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}