Social cards in pure Rust: no headless Chrome
Cloud Cost Analyzer (CCA) lets you share a read-only scan as a link. Someone drops that link in Slack or on LinkedIn, and instead of a bare URL I wanted it to unfurl into a proper card: the brand mark, the monthly savings we found, the number of findings. That was two separate problems. Figuring them out took me somewhere I didn't expect, because neither one wanted a headless browser, but both…
Cloud Cost Analyzer (CCA) shares read-only scans as links on platforms like Slack, LinkedIn, and X. However, the links initially unfurl into a bare URL instead of a proper card displaying brand marks, savings, and findings. To solve this issue, Cloud Cost Analyzer implemented a user-agent-based dynamic rendering technique for social crawlers.
When a crawler visits the link, it is redirected to a small server-rendered /embed page with Open Graph tags. This allows the crawler to read a fully-formed page with og:title, og:image, and other necessary metadata while human users continue to interact with the interactive dashboard.
To create the social cards, CCA built an SVG string and rasterized it in-process using pure Rust libraries, resvg and tiny-skia. This approach eliminates the need for a headless browser and the associated costs, such as shipping a browser alongside the service and keeping it patched. The renderer is about ten lines of code, with the layout being single-column and left-aligned for simplicity.
The SVG references fonts using the CSS way, finding a family named exactly "Space Grotesk" and picking the closest weight within that family. A slight font issue was resolved by normalizing the font files' name tables, ensuring consistent rendering across different faces.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.