fix(preview): destroy pdf.js document on recompile

The preview created a new PDF document on every recompile but never
destroyed the old one, leaking the document and its worker transport.
Keep the loading task and destroy it on effect cleanup.

Claude-Session: https://claude.ai/code/session_01RzTWrLiy4zGuw2hZSQGamk
This commit is contained in:
pj committed 2026-06-22 13:29:30 -04:00
1 parent 4e5ea6d855
commit aa75b3ebda
1 file changed
+3 -1
+3 -1
View File
@@ -12,10 +12,11 @@ export function PdfPreview({ data }: { data: Uint8Array }) {
const container = ref.current;
if (!container) return;
let cancelled = false;
const loadingTask = pdfjs.getDocument({ data: data.slice() });
(async () => {
try {
const doc = await pdfjs.getDocument({ data: data.slice() }).promise;
const doc = await loadingTask.promise;
if (cancelled) return;
const width = container.clientWidth;
const dpr = window.devicePixelRatio || 1;
@@ -47,6 +48,7 @@ export function PdfPreview({ data }: { data: Uint8Array }) {
return () => {
cancelled = true;
loadingTask.destroy();
};
}, [data]);