From 95ae0d9bca1450f4498ee4d354cfb0eab0573d80 Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Sat, 18 Jul 2026 00:15:12 +0200 Subject: [PATCH] fix(pdf): contain render failures instead of crashing the app --- src/app/components/Pdf-viewer/PdfViewer.tsx | 26 +++++++++++++++------ src/app/plugins/pdfjs-dist.ts | 4 ++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/app/components/Pdf-viewer/PdfViewer.tsx b/src/app/components/Pdf-viewer/PdfViewer.tsx index d3cddbae2..094a85cc7 100644 --- a/src/app/components/Pdf-viewer/PdfViewer.tsx +++ b/src/app/components/Pdf-viewer/PdfViewer.tsx @@ -62,8 +62,11 @@ export const PdfViewer = as<'div', PdfViewerProps>( ); const isLoading = pdfJSState.status === AsyncStatus.Loading || docState.status === AsyncStatus.Loading; + const [renderError, setRenderError] = useState(false); const isError = - pdfJSState.status === AsyncStatus.Error || docState.status === AsyncStatus.Error; + pdfJSState.status === AsyncStatus.Error || + docState.status === AsyncStatus.Error || + renderError; const [pageNo, setPageNo] = useState(1); const [jumpAnchor, setJumpAnchor] = useState(); @@ -77,17 +80,26 @@ export const PdfViewer = as<'div', PdfViewerProps>( }, [pdfJSState, loadPdfDocument]); useEffect(() => { - if (docState.status === AsyncStatus.Success) { - const doc = docState.data; - if (pageNo < 0 || pageNo > doc.numPages) return; - createPage(doc, pageNo, { scale: zoom }).then((canvas) => { + if (docState.status !== AsyncStatus.Success) return undefined; + const doc = docState.data; + if (pageNo < 0 || pageNo > doc.numPages) return undefined; + let cancelled = false; + setRenderError(false); + createPage(doc, pageNo, { scale: zoom }) + .then((canvas) => { + if (cancelled) return; const container = containerRef.current; if (!container) return; container.textContent = ''; container.append(canvas); canvas.style.touchAction = 'pan-x pan-y'; + }) + .catch(() => { + if (!cancelled) setRenderError(true); }); - } + return () => { + cancelled = true; + }; }, [docState, pageNo, zoom]); const handleDownload = () => { @@ -186,7 +198,7 @@ export const PdfViewer = as<'div', PdfViewerProps>( )} - {docState.status === AsyncStatus.Success && ( + {docState.status === AsyncStatus.Success && !renderError && (