import { createMemo, Show } from "solid-js"; import { getRequestEvent, isServer, NoHydration } from "@solidjs/web"; import { Link, Meta } from "@solidjs/meta"; import { useLocation } from "@solidjs/router"; import { DynamicImage, urlFromImage } from "@solidjs/ogp"; import { useFrontmatter, useSolidBaseContext } from "@kobalte/solidbase/client"; import { ogFonts } from "./og-fonts"; import { ogStaticPath } from "../lib/og-path"; import { ogAssetUrl } from "./og-assets"; /** * Open Graph / Twitter card tags for every page, with a generated preview image. * * Each `` below is rewritten by the `@solidjs/ogp` compiler plugin (vite.config.ts) * into a GET server function served from `/_server`. On the page it becomes the URL of that * function, with every identifier/call/member expression used inside the JSX sent along as a * query argument. So: * * - Only pass short JSON-safe strings/numbers into the JSX (the whole URL must stay < 2000 chars). * Literals (static text, style values) stay in the server function and cost nothing. * - Images are rendered by satori: flexbox only, and any element with more than one child needs * `display: flex`. * - Each card is wrapped in . The compiled template asks for a hydration key, and * once the server process has rendered any page, Solid's `sharedConfig.context` is left set, * so outside an owner that throws ("getNextContextId cannot be used under non-hydrating * context") and the endpoint returns 500. NoHydration supplies an owner and turns keys off * (@solidjs/ogp 0.1.0). * - `imageOptions` must be an inline object literal; the compiler copies it into the server * function verbatim, so `ogFonts()` there runs on the server only. * * Pages are prerendered, so crawlers read the tags straight from the static HTML. The images are * rendered once at build time: `scripts/static-og.ts` runs after `vite build`, requests each * `/_server` image URL from dist/server/server.js, saves the PNG under `/og/`, and points the * page's og:image / twitter:image tags at that static file. Nothing renders images at request * time, so the deployed site needs no server for them. */ const WIDTH = 1200; const HEIGHT = 630; /** Matches the `.stage-badge.stage-N` colors in app.css. */ const STAGE_COLORS: Record = { 0: { bg: "#fa233e", fg: "#ffffff" }, 1: { bg: "#ffa15c", fg: "#ffffff" }, 2: { bg: "#e9de47", fg: "#1a1a1a" }, 3: { bg: "#12c3a2", fg: "#ffffff" }, 4: { bg: "#2962ff", fg: "#ffffff" }, }; /** Keeps the description to roughly three lines on the card (and the image URL short). */ function truncate(text: string, max: number): string { if (text.length <= max) return text; return `${text.slice(0, max - 1).replace(/\s+\S*$/, "")}…`; } /** * Solid's SSR escapes `&` and `<` in text, and @solidjs/ogp hands that HTML to satori-html, which * doesn't decode entities, so they'd render literally as `&` / `<` (ogp 0.1.0). * Swap them for lookalikes until that's fixed upstream. */ function forImage(text: string): string { return text.replace(/\s*&\s*/g, " and ").replace(/ into URL arguments) and // they render inside the server function. Values: --pl-primary #017ad4, --sb-heading-color // hsl(240 6% 10%) = #18181b, --sb-text-color hsl(240 5% 26%) = #3f3f46. // --------------------------------------------------------------------------------------------- /** * The site's `html` background: radial glow, 48px grid, then a fade to white. Even these empty * layers need `display: flex`; satori-html's output otherwise fails satori's multi-child check. */ function Backdrop() { return ( <>
); } /** Header logo + the "v2.0" badge (`[class*="version-selector_"]::after` in app.css). */ function Brand() { return (
v2.0
); } /** The hero's wireframe cube (`.pl-hero-cube`). */ function Cube() { return ( ); } function packageCardUrl(fm: PackageFrontmatter): string { const title = forImage(fm.title ?? fm.package ?? ""); // Long names ("Intersection Observer") would wrap at the full size and push the stats down. const titleSize = title.length > 16 ? "64px" : "84px"; const name = `@solid-primitives/${fm.package}`; const description = forImage(truncate(fm.description ?? "", 110)); const eyebrow = `— ${forImage(fm.category ?? "Primitives")}`; const stage = String(fm.stage ?? 0); const stageBg = STAGE_COLORS[fm.stage ?? 0]?.bg ?? STAGE_COLORS[0]!.bg; const stageFg = STAGE_COLORS[fm.stage ?? 0]?.fg ?? "#ffffff"; const count = String(fm.primitives ?? 0); const countLabel = fm.primitives === 1 ? "primitive" : "primitives"; return urlFromImage(
{eyebrow}
{title}
{name}
{description}
{count}
{countLabel}
{stage}
Stage
, )(); } function defaultCardUrl(): string { // Text with non-ASCII characters goes in as whole values. Written statically inside // , Solid compiles "—" to `—`, which satori-html renders literally; and // static text mixed with an inserted value in one element loses everything before the value. const eyebrow = "— A reactive primitives library for Solid 2.0"; const subtitle = "Composable, tree-shakeable primitive packages — signals, browser APIs, animation, and more."; return urlFromImage(
{eyebrow}
You write the app logic.
We ship the primitives that keep up.
{subtitle}
, )(); } export function SiteOpenGraph() { const frontmatter = useFrontmatter(); const { config, metaTitle } = useSolidBaseContext(); const location = useLocation(); const origin = siteOrigin(); // Inside a memo so client-side navigation re-evaluates the image arguments: the compiled // captures its argument values when it's called. // // In a production build the card is a static file (scripts/static-og.ts). The server render keeps // the `/_server` URL in the HTML, which that script finds, renders and rewrites. The client // computes the same static path itself: `@solidjs/meta` re-evaluates this after hydration and on // every navigation, and the static host has no `/_server` to answer the original URL. const image = createMemo(() => { const fm = frontmatter(); const renderUrl = fm?.package ? packageCardUrl(fm) : defaultCardUrl(); return ( origin + (isServer || import.meta.env.DEV ? renderUrl : ogStaticPath(renderUrl, __OG_VERSION__)) ); }); const description = () => frontmatter()?.description ?? config().description; const imageAlt = () => { const fm = frontmatter(); return fm?.package ? `${fm.title ?? fm.package} (@solid-primitives/${fm.package}) — Solid Primitives` : `${config().title} — reactive primitives for Solid`; }; return ( <> {d => } {d => } ); }