SnapDOMGitHub★ 8K
Documentation
zumerlab/snapdom …

SnapDOM Cache Behavior

Reuse captured results, assets and styles across calls. snapdom.preCapture() can prepare an eligible capture when a user focuses or approaches its control.

Cache control preCapture()
Three kinds of reuse

A result supports multiple exports of the same captured state. A later snapdom(el) call may reuse an unchanged capture. Resource and style caches also reduce work when a fresh capture is needed; the cache option controls those caches.

Cache control

Leave cache at its default for normal use. The other modes support debugging or older callers:

ModeDescription
"soft"Content-keyed persistent resource/style caches are enabled (default).
"disabled" / falseClears and bypasses those persistent caches. A debug and testing escape, not a tuning option.
"auto" / "full"Accepted for v2 compatibility and silently mapped to "soft".

The policy is consulted when the capture pipeline actually runs. It does not disable automatic repeat memoization; a memo hit performs no resource/style-cache work at all.

Example:

// Clear and bypass persistent resource/style caches (debugging)
await snapdom.toPng(el, { cache: 'disabled' });

Eligible unchanged captures reuse the saved result. Observable changes invalidate it; supported local changes rebuild the affected subtree, while other changes trigger a full capture. Canvas, video and other frame-driven content capture fresh. After a CSSOM edit such as sheet.insertRule() or rule.style.color = 'red', pass invalidate: true because those edits have no mutation event. It clears style snapshots and takes a fresh capture, which may become the next reusable result.

Callbacks that read application state: function-valued filter, exclude, excludeStyleProps and fallbackURL suspend capture memoization. Each new capture reevaluates applicable callbacks, including their style or fallback decisions; changing a closure does not require invalidate.

let privateMode = false;
const options = {
  exclude: node => privateMode && node.matches('[data-private]'),
  excludeMode: 'remove'
};
const before = await snapdom(el, options);
privateMode = true;
const after = await snapdom(el, options); // current policy, no invalidate needed

Exporting before again still exports the earlier content. Use the new result after when policy changes. Callbacks run where applicable during capture; do not depend on an exact number of invocations.

V2 migration: remove the public burst switch. Eligible repeat memoization is automatic in v3. Resource-cache settings and image exports remain separate from this reuse policy; cache: 'disabled' is not a replacement for burst: false.

preCapture()

Call snapdom.preCapture() once during setup. When an eligible capture starts in a press or click handler, SnapDOM remembers that control, target and a shallow copy of the call's options. Later pointer-enter, focus or press events on that control can prepare the same capture before the handler needs it. Normal invalidation still applies. The first intent event on an unknown control also warms the visible viewport once.

import { snapdom } from '@zumer/snapdom';

snapdom.preCapture();

// The first click teaches the association; later intent can prepare the capture.
button.onclick = () => snapdom.toPng(hero, { scale: 2 });

preCapture() takes no arguments and reacts to intent events, with no polling. Programmatic callers can capture early themselves. The old preCache() API was removed in v3.

Ready to capture?

Try the browser demo, then use the API reference to choose your output.

Open the demo Install from npm