Skip to content
SnapDOMTry SnapStudioGitHub★ 8K
Next.js Guide

Capture HTML to PNG in Next.js

Use a client component and a dynamic import to capture after hydration, loading SnapDOM when the user exports.

TL;DR

SnapDOM needs a browser DOM. Use a client component for the ref and event handler, then import SnapDOM inside the handler. This keeps capture code out of server rendering.

1. Install

npm install @zumer/snapdom@latest

Run the capture in a browser event handler after the component has hydrated.

2. App Router: client component

Create a client component that owns the ref and capture logic. 'use client' defines a client boundary, but client components may still be prerendered. Importing SnapDOM inside the click handler keeps that import in the browser.

// app/capture/CaptureCard.tsx
'use client';

import { useRef } from 'react';

export default function CaptureCard() {
  const cardRef = useRef<HTMLDivElement>(null);

  async function handleCapture() {
    if (!cardRef.current) return;
    const { snapdom } = await import('@zumer/snapdom');
    const img = await snapdom.toPng(cardRef.current, { scale: 2 });
    document.body.appendChild(img);
  }

  return (
    <div>
      <div ref={cardRef} className="card">
        <h3>Hello SnapDOM</h3>
        <p>Captured from a Next.js client component.</p>
      </div>
      <button onClick={handleCapture}>Capture</button>
    </div>
  );
}

Try it live

This plain HTML demo uses the same capture call as the client component. In Next.js, the button becomes available after hydration.

Hello SnapDOM

Captured from a Next.js client component.

The captured PNG will appear here.

3. Use it from a server page

Server components can render this client component as a child. The 'use client' boundary allows its event handler and ref; the dynamic import inside the handler runs when the user captures:

// app/capture/page.tsx
import CaptureCard from './CaptureCard';

export default function Page() {
  return (
    <main>
      <h1>SnapDOM in Next.js</h1>
      <CaptureCard />
    </main>
  );
}

4. Alternative: dynamic import (lazy load)

The handler above already loads SnapDOM on demand. You can also load the entire capture component dynamically with SSR disabled. Put this dynamic() call inside a client component:

// app/capture/CaptureLazy.tsx
'use client';

import dynamic from 'next/dynamic';

export const CaptureCard = dynamic(() => import('./CaptureCard'), {
  ssr: false,
  loading: () => <p>Loading capture tool…</p>,
});

Your server page then imports CaptureCard from this wrapper. In the pages router (pages/capture.tsx) there's no 'use client' directive and no restriction — ssr: false works directly in the page file.

5. Upload a share image

Capture a rendered share card to a PNG Blob and upload it to your endpoint. The endpoint must store the file and return its public URL:

async function share() {
  if (!cardRef.current) return;
  const { snapdom } = await import('@zumer/snapdom');
  const blob = await snapdom.toBlob(cardRef.current, { scale: 2, format: 'png' });

  const form = new FormData();
  form.append('file', blob, 'share.png');

  const response = await fetch('/api/upload', { method: 'POST', body: form });
  if (!response.ok) throw new Error('Image upload failed');
  const { url } = await response.json();
  return url;
}

For an Open Graph preview, the shared URL must serve HTML that references this stored image. A client-side capture alone does not update that page. For server-rendered cards, see the @vercel/og comparison below.

Common gotchas

"window is not defined" / "document is not defined" errors

Import SnapDOM inside a browser event handler, as shown above, or load the capture component with dynamic(() => import('./CaptureCard'), { ssr: false }) from a client component. A 'use client' directive alone does not prevent prerendering.

The captured image is blank in production but works in dev

Check that the target is mounted, has a nonzero size and contains the expected data. Wait for layout-changing assets such as fonts. SnapDOM embeds used web fonts with embedFonts: 'auto' by default; a blank capture can have causes beyond font loading.

Should I use SnapDOM or @vercel/og for OG images?

Different tools. @vercel/og renders Satori-compatible JSX on the server (no real browser, no full CSS). SnapDOM captures rendered DOM in the browser, including the current component state and browser layout.

Use @vercel/og when the card is fully described in JSX and you don't need real browser fidelity. Use SnapDOM when you want to capture a piece of your live UI with its current browser layout.

Can I run SnapDOM in a server action?

No. Server actions run in Node.js without a real DOM. Use Puppeteer or Playwright if you need server-side screenshots, and SnapDOM client-side.

Cross-origin images break the capture

Use CORS-enabled image responses or a proxy you control via { useProxy: 'https://your-proxy.example.com/' }. A configured proxy handles cross-origin asset requests; same-origin requests remain direct.

Does it work with Turbopack?

SnapDOM has an ES module entry and no native runtime dependencies. Keep its import and capture calls inside the browser boundary when using Turbopack or Webpack.