Back to KB
Difficulty
Intermediate
Read Time
8 min

Dynamic OG Images for Next.js & Vercel (3 Production Patterns)

By Codcompass Team··8 min read

Scaling Dynamic Social Previews: Architecture Patterns for Next.js OG Images

Current Situation Analysis

Social platforms rely on Open Graph (OG) images to provide visual context when URLs are shared. For static marketing pages, a single hardcoded image suffices. However, modern applications are dynamic. Blog posts, product listings, user profiles, and data dashboards change per request. Serving a generic preview for these routes kills engagement; users ignore links that lack specific context.

The industry pain point is not just generating these images, but doing so without degrading the user experience. Social crawlers (Twitter/X, LinkedIn, Slack) have strict timeouts. If your OG image endpoint takes too long to respond, the platform may drop the image entirely, resulting in a broken preview.

This problem is often misunderstood as purely a rendering challenge. Developers frequently select a tool based on rendering capability alone, ignoring infrastructure trade-offs. The reality is a triad of constraints:

  1. Rendering Fidelity: Can the tool reproduce the desired visual design?
  2. Latency: Can the image be generated within the crawler's timeout window?
  3. Operational Overhead: What is the cost of maintenance, external dependencies, and caching complexity?

Data from production deployments shows that @vercel/og (backed by Satori) typically renders images in under 50ms at the edge, while screenshot-based approaches often introduce 300ms to 800ms of latency due to browser initialization and network hops. Yet, Satori only supports a subset of CSS. Choosing the wrong pattern leads to either slow previews or visual drift where the social card looks nothing like the actual page.

WOW Moment: Key Findings

The decision between generation strategies is rarely binary. It is a trade-off matrix involving latency, fidelity, and infrastructure complexity. The following comparison highlights the operational reality of each pattern in a production Next.js environment.

StrategyRender LatencyCSS FidelityInfrastructure ComplexityPrimary Risk
Edge / Satori< 50msSubset (Flexbox, limited grid)Low (Native to Next.js)Visual limitations on complex UI
Screenshot API300ms - 800ms+Full (Browser parity)High (External dependency)Cold starts, API rate limits, cost
Hybrid RoutingVariableMixedMedium (Logic overhead)Cache key fragmentation, routing bugs

Why this matters: Most applications do not need a single strategy. High-traffic, simple routes (e.g., blog posts) benefit from edge speed, while low-frequency, complex routes (e.g., a user's custom dashboard share) can tolerate screenshot latency. A hybrid approach allows you to optimize for the 80/20 rule, keeping the critical path fast while preserving fidelity where it counts.

Core Solution

Implementing dynamic OG images requires selecting a generation strategy that aligns with your content complexity and traffic patterns. Below are the three production-grade patterns, implemented with distinct architectural choices.

Pattern 1: Edge Runtime Generation with @vercel/og

This pattern leverages the Vercel Edge Runtime to generate images using Satori. Satori converts JSX and CSS into SVG, which is then rasterized to PNG. This is the default recommendation for most applications due to its speed and zero external dependencies.

Architecture Rationale:

  • Runtime: edge ensures the function runs close to the user/crawler with no cold starts.
  • Rendering: Satori is lightweight but restrictive. It supports flexbox, basic grid, and standard CSS properties. It does not support complex pseudo-selectors, advanced filters, or arbitrary JavaScript execution.
  • Best For: Text-heavy cards, branding elements, logos, an

🎉 Mid-Year Sale — Unlock Full Article

Base plan from just $4.99/mo or $49/yr

Sign in to read the full article and unlock all 635+ tutorials.

Sign In / Register — Start Free Trial

7-day free trial · Cancel anytime · 30-day money-back