perf(ui): lazy-load html-to-image and snapDOM

Both are only needed on explicit user actions (export message as image,
capture preview annotation screenshot). Defer them with dynamic import so
~140 KB leaves the eager app-shell graph.

Co-authored-by: Serhii Dziupin <makeittech@users.noreply.github.com>
This commit is contained in:
Cursor Agent
2026-06-26 11:26:11 +00:00
co-authored by Serhii Dziupin
parent 3d27c0a0ca
commit 05dd270a9a
2 changed files with 8 additions and 3 deletions
@@ -33,7 +33,6 @@ import { copyTextToClipboard } from '@/lib/clipboard';
import { useChatSurfaceMode } from '@/components/chat/useChatSurfaceMode';
import { isVSCodeRuntime } from '@/lib/desktop';
import { useRuntimeAPIs } from '@/hooks/useRuntimeAPIs';
import { toPng } from 'html-to-image';
import { toast } from '@/components/ui';
import { Icon } from "@/components/icon/Icon";
import { formatTimestampForDisplay } from './timeFormat';
@@ -1455,6 +1454,9 @@ const AssistantMessageBody = React.memo(({
wrapper.appendChild(clone);
document.body.appendChild(wrapper);
// Lazy-load html-to-image: only needed when the user exports a
// message as an image, so keep it out of the eager app shell.
const { toPng } = await import('html-to-image');
const dataUrl = await toPng(wrapper, {
quality: 1,
pixelRatio: 2,
@@ -1,5 +1,3 @@
import { snapdom } from '@zumer/snapdom';
import { getFontEmbedCSS, toJpeg } from 'html-to-image';
import { invokeDesktop } from '@/lib/desktop';
import { runtimeFetch } from '@/lib/runtime-fetch';
@@ -825,6 +823,9 @@ async function captureIframeSnapdomScreenshot(
// Defensive: undo any nested-scroll drift from asset inlining before capture.
nestedScroll.reapply();
// Lazy-load snapDOM: only needed when actually capturing a preview
// annotation screenshot, so keep it out of the eager app shell.
const { snapdom } = await import('@zumer/snapdom');
const snapdomOptions = {
backgroundColor: getCaptureBackgroundColor(document),
cache: 'disabled' as const,
@@ -924,6 +925,8 @@ async function captureIframeDomScreenshot(
await document.fonts?.ready.catch(() => undefined);
restoreAssets = await inlineIframeCaptureAssets(document, viewportWidth, viewportHeight, { applyLayoutWorkarounds: true });
frameWindow.scrollTo(scrollX, scrollY);
// Lazy-load html-to-image: only the fallback DOM-capture path needs it.
const { getFontEmbedCSS, toJpeg } = await import('html-to-image');
const fontEmbedCSS = await getFontEmbedCSS(root).catch(() => '');
dataUrl = await toJpeg(root, {