diff --git a/src/frontend/src/utils/og-image-renderer.ts b/src/frontend/src/utils/og-image-renderer.ts index 7e76204930..8cce3b46f2 100644 --- a/src/frontend/src/utils/og-image-renderer.ts +++ b/src/frontend/src/utils/og-image-renderer.ts @@ -24,6 +24,15 @@ import type { TopicMetadata } from './topic-resolver.ts'; * Buffers (fonts + background image) are loaded once and cached because * every static path renders through the same module instance. * + * Rasterizing satori's SVG with librsvg dominates the cost of each card, so + * the layers that never change (background, gradient, and the CTA chip with + * its drop shadow) are rasterized once into a cached base layer. Each card + * then only rasterizes its own overlay and composites it onto that base. The + * per-card elements intentionally have no `boxShadow`: satori emits shadows + * as SVG blur filters, which made up most of the render time while being + * imperceptible against the dark gradient behind those elements. + * See `stripRedundantOverflowMasks` for the other rasterization shortcut. + * * Paths are resolved against `process.cwd()` (which Astro sets to the * frontend project root at build time) rather than `import.meta.url` so * that the renderer continues to work after Vite bundles this module into @@ -47,6 +56,15 @@ const CTA_LABEL = 'Read on aspire.dev →'; let regularFontPromise: Promise | undefined; let boldFontPromise: Promise | undefined; let backgroundDataUriPromise: Promise | undefined; +let fontsPromise: Promise | undefined; +let baseLayerPromise: Promise | undefined; + +type SatoriFont = Parameters[1]['fonts'][number]; + +interface RawImage { + data: Buffer; + info: { width: number; height: number; channels: 3 }; +} export interface RenderOgImageInput { title: string; @@ -55,26 +73,41 @@ export interface RenderOgImageInput { } export async function renderOgImagePng(input: RenderOgImageInput): Promise { - const [regularFont, boldFont, backgroundDataUri] = await Promise.all([ - loadRegularFont(), - loadBoldFont(), - loadBackgroundDataUri(), + const [baseLayer, overlaySvg] = await Promise.all([ + loadBaseLayer(), + renderSvg( + buildOverlayTree({ + title: input.title, + description: input.description?.trim() ? input.description.trim() : undefined, + topic: input.topic, + }) + ), ]); - const tree = buildTree({ - title: input.title, - description: input.description?.trim() ? input.description.trim() : undefined, - topic: input.topic, - backgroundDataUri, - }); + // Cards are fully opaque, so emit RGB rather than RGBA (less to encode, smaller files). + return sharp(baseLayer.data, { raw: baseLayer.info }) + .composite([{ input: Buffer.from(stripRedundantOverflowMasks(overlaySvg)) }]) + .removeAlpha() + .png({ compressionLevel: 6 }) + .toBuffer(); +} - const svg = await satori(tree, { +/** + * Satori clips `overflow: hidden` elements with both a rectangular `clip-path` + * and an equivalent rectangular `mask`. librsvg renders every mask to an + * offscreen surface, which more than doubles the overlay's rasterization time, + * so drop the mask reference wherever the same element already has a clip-path. + * If satori's output ever changes shape this simply stops matching. + */ +function stripRedundantOverflowMasks(svg: string): string { + return svg.replace(/ mask="url\(#satori_om-[^)]+\)"(?=[^>]* clip-path=")/g, ''); +} + +async function renderSvg(tree: ReturnType): Promise { + return satori(tree, { width: DEFAULT_OG_IMAGE_WIDTH, height: DEFAULT_OG_IMAGE_HEIGHT, - fonts: [ - { name: 'Outfit', data: regularFont, weight: 400, style: 'normal' }, - { name: 'Outfit', data: boldFont, weight: 700, style: 'normal' }, - ], + fonts: await loadFonts(), loadAdditionalAsset: async (code, segment) => { if (code === 'emoji') { return loadEmojiSvgDataUri(segment); @@ -82,8 +115,32 @@ export async function renderOgImagePng(input: RenderOgImageInput): Promise { + if (!baseLayerPromise) { + baseLayerPromise = (async () => { + const svg = await renderSvg(buildBaseTree(await loadBackgroundDataUri())); + const { data, info } = await sharp(Buffer.from(svg)) + .removeAlpha() + .raw() + .toBuffer({ resolveWithObject: true }); + return { data, info: { width: info.width, height: info.height, channels: 3 } }; + })(); + } + return baseLayerPromise; +} + +// Satori caches its font loader per `fonts` array instance, so reuse one array. +function loadFonts(): Promise { + if (!fontsPromise) { + fontsPromise = Promise.all([loadRegularFont(), loadBoldFont()]).then(([regular, bold]) => [ + { name: 'Outfit', data: regular, weight: 400, style: 'normal' }, + { name: 'Outfit', data: bold, weight: 700, style: 'normal' }, + ]); + } + return fontsPromise; } const TWEMOJI_BASE = 'https://cdn.jsdelivr.net/gh/twitter/twemoji@latest/assets/svg'; @@ -169,14 +226,13 @@ function loadBackgroundDataUri(): Promise { return backgroundDataUriPromise; } -interface TreeInput { +interface OverlayTreeInput { title: string; description?: string; topic: TopicMetadata; - backgroundDataUri: string; } -function buildTree({ title, description, topic, backgroundDataUri }: TreeInput) { +function rootNode(children: unknown[]) { return { type: 'div', props: { @@ -187,61 +243,70 @@ function buildTree({ title, description, topic, backgroundDataUri }: TreeInput) display: 'flex', fontFamily: 'Outfit', }, - children: [ - { - type: 'img', - props: { - src: backgroundDataUri, - width: DEFAULT_OG_IMAGE_WIDTH, - height: DEFAULT_OG_IMAGE_HEIGHT, - style: { - position: 'absolute', - top: 0, - left: 0, - objectFit: 'cover', - }, - }, + children, + }, + }; +} + +function buildBaseTree(backgroundDataUri: string) { + return rootNode([ + { + type: 'img', + props: { + src: backgroundDataUri, + width: DEFAULT_OG_IMAGE_WIDTH, + height: DEFAULT_OG_IMAGE_HEIGHT, + style: { + position: 'absolute', + top: 0, + left: 0, + objectFit: 'cover', }, - // Bottom-to-top gradient ensures the overlay card has high contrast - // against bright background imagery. - { - type: 'div', - props: { - style: { - position: 'absolute', - top: 0, - left: 0, - right: 0, - bottom: 0, - display: 'flex', - backgroundImage: - 'linear-gradient(to top, rgba(15,14,30,0.92) 0%, rgba(15,14,30,0.45) 55%, rgba(15,14,30,0) 100%)', - }, - }, + }, + }, + // Bottom-to-top gradient ensures the overlay card has high contrast + // against bright background imagery. + { + type: 'div', + props: { + style: { + position: 'absolute', + top: 0, + left: 0, + right: 0, + bottom: 0, + display: 'flex', + backgroundImage: + 'linear-gradient(to top, rgba(15,14,30,0.92) 0%, rgba(15,14,30,0.45) 55%, rgba(15,14,30,0) 100%)', }, - { - type: 'div', - props: { - style: { - position: 'absolute', - left: 56, - right: 56, - bottom: 56, - display: 'flex', - flexDirection: 'column', - gap: 22, - }, - children: [ - topicPill(topic), - titleBubble(title), - description ? descriptionBubble(description) : null, - ].filter(Boolean), - }, + }, + }, + ctaChip(), + ]); +} + +function buildOverlayTree({ title, description, topic }: OverlayTreeInput) { + return rootNode([ + { + type: 'div', + props: { + style: { + position: 'absolute', + left: 56, + right: 56, + bottom: 56, + display: 'flex', + flexDirection: 'column', + gap: 22, }, - ctaChip(), - ], + children: [ + topicPill(topic), + titleBubble(title), + description ? descriptionBubble(description) : null, + ].filter(Boolean), + }, }, - }; + ]); } function topicPill(topic: TopicMetadata) { @@ -259,7 +324,6 @@ function topicPill(topic: TopicMetadata) { padding: '12px 26px 12px 18px', fontSize: 28, fontWeight: 600, - boxShadow: '0 8px 24px rgba(0, 0, 0, 0.32)', }, children: [ { @@ -317,7 +381,6 @@ function titleBubble(title: string) { borderRadius: 28, padding: '32px 40px', maxWidth: '100%', - boxShadow: '0 18px 48px rgba(0, 0, 0, 0.45)', }, children: { type: 'div', @@ -351,7 +414,6 @@ function descriptionBubble(description: string) { borderRadius: 22, padding: '20px 32px', maxWidth: '92%', - boxShadow: '0 12px 32px rgba(0, 0, 0, 0.35)', }, children: { type: 'div', @@ -403,5 +465,7 @@ export function __setRendererCachesForTests(overrides: { backgroundDataUriPromise = overrides.background ? Promise.resolve(overrides.background) : backgroundDataUriPromise; + fontsPromise = undefined; + baseLayerPromise = undefined; }