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 && (