From fa6cb5227b6d12af9257c5948119bed6f6081286 Mon Sep 17 00:00:00 2001 From: David Pine <7679720+IEvangelist@users.noreply.github.com> Date: Tue, 17 Nov 2026 07:58:18 -0600 Subject: [PATCH 1/2] Speed up OG image rendering ~5.7x Rasterize the constant layers (background, gradient, CTA chip) once into a cached raw base layer and composite each card's overlay onto it, instead of re-embedding and re-decoding the 400 KB background PNG for every card. Drop box-shadows from the per-card elements: satori emits them as SVG blur filters, which accounted for ~65% of librsvg rasterization time while being imperceptible against the dark gradient. The CTA chip keeps its shadow since it is baked into the base layer. Also reuse a single satori fonts array and use PNG compression level 6. Measured per card: 296 ms -> 52 ms sequential, 68 ms -> 12 ms concurrent; output is visually identical (mean pixel diff ~1/255) and ~6% smaller. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- src/frontend/src/utils/og-image-renderer.ts | 196 ++++++++++++-------- 1 file changed, 123 insertions(+), 73 deletions(-) diff --git a/src/frontend/src/utils/og-image-renderer.ts b/src/frontend/src/utils/og-image-renderer.ts index 7e76204930..205bd0d914 100644 --- a/src/frontend/src/utils/og-image-renderer.ts +++ b/src/frontend/src/utils/og-image-renderer.ts @@ -24,6 +24,14 @@ 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. + * * 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 +55,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: 4 }; +} export interface RenderOgImageInput { title: string; @@ -55,26 +72,28 @@ 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, - }); + return sharp(baseLayer.data, { raw: baseLayer.info }) + .composite([{ input: Buffer.from(overlaySvg) }]) + .png({ compressionLevel: 6 }) + .toBuffer(); +} - const svg = await satori(tree, { +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 +101,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)) + .ensureAlpha() + .raw() + .toBuffer({ resolveWithObject: true }); + return { data, info: { width: info.width, height: info.height, channels: 4 } }; + })(); + } + return baseLayerPromise; +} - return sharp(Buffer.from(svg)).png({ compressionLevel: 9 }).toBuffer(); +// 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 +212,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 +229,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 +310,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 +367,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 +400,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 +451,7 @@ export function __setRendererCachesForTests(overrides: { backgroundDataUriPromise = overrides.background ? Promise.resolve(overrides.background) : backgroundDataUriPromise; + fontsPromise = undefined; + baseLayerPromise = undefined; } From f99bbdbafb4a2ea23f2f85456f603efa1bff748c Mon Sep 17 00:00:00 2001 From: David Pine <7679720+IEvangelist@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:01:54 -0600 Subject: [PATCH 2/2] Cut OG overlay rasterization cost further Strip satori's overflow mask references where the same element already has an equivalent rectangular clip-path. librsvg renders each mask offscreen, which more than doubled overlay rasterization time; output is pixel-identical. Keep the base layer and output as RGB since cards are fully opaque, which reduces PNG encode work and file size (~5% smaller). Per card: 51 ms -> 37 ms sequential (~8x faster than the original renderer). Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- src/frontend/src/utils/og-image-renderer.ts | 22 +++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/frontend/src/utils/og-image-renderer.ts b/src/frontend/src/utils/og-image-renderer.ts index 205bd0d914..8cce3b46f2 100644 --- a/src/frontend/src/utils/og-image-renderer.ts +++ b/src/frontend/src/utils/og-image-renderer.ts @@ -31,6 +31,7 @@ import type { TopicMetadata } from './topic-resolver.ts'; * 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 @@ -62,7 +63,7 @@ type SatoriFont = Parameters[1]['fonts'][number]; interface RawImage { data: Buffer; - info: { width: number; height: number; channels: 4 }; + info: { width: number; height: number; channels: 3 }; } export interface RenderOgImageInput { @@ -83,12 +84,25 @@ export async function renderOgImagePng(input: RenderOgImageInput): Promise]* clip-path=")/g, ''); +} + async function renderSvg(tree: ReturnType): Promise { return satori(tree, { width: DEFAULT_OG_IMAGE_WIDTH, @@ -109,10 +123,10 @@ function loadBaseLayer(): Promise { baseLayerPromise = (async () => { const svg = await renderSvg(buildBaseTree(await loadBackgroundDataUri())); const { data, info } = await sharp(Buffer.from(svg)) - .ensureAlpha() + .removeAlpha() .raw() .toBuffer({ resolveWithObject: true }); - return { data, info: { width: info.width, height: info.height, channels: 4 } }; + return { data, info: { width: info.width, height: info.height, channels: 3 } }; })(); } return baseLayerPromise;