fix(frontend): load pdfjs-dist dynamically to avoid SSR crash (#39)
Static import of pdfjs-dist fails during SSR because DOMMatrix and other browser globals are unavailable in Node.js. Move the import into onMount so it only ever executes in the browser. A plain pdfjsLib variable holds the module; a $state boolean pdfjsReady triggers the load-document effect once the library is available. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { getDocument, GlobalWorkerOptions, TextLayer } from 'pdfjs-dist';
|
import { onMount } from 'svelte';
|
||||||
import type { PDFDocumentProxy, PDFPageProxy, RenderTask } from 'pdfjs-dist';
|
import type { PDFDocumentProxy, PDFPageProxy, RenderTask } from 'pdfjs-dist';
|
||||||
import workerUrl from 'pdfjs-dist/build/pdf.worker.min.mjs?url';
|
|
||||||
|
|
||||||
GlobalWorkerOptions.workerSrc = workerUrl;
|
|
||||||
|
|
||||||
let { url }: { url: string } = $props();
|
let { url }: { url: string } = $props();
|
||||||
|
|
||||||
@@ -22,7 +19,24 @@ let textLayerEl = $state<HTMLDivElement | null>(null);
|
|||||||
let renderTask: RenderTask | null = null;
|
let renderTask: RenderTask | null = null;
|
||||||
let textLayerInstance: { cancel: () => void } | null = null;
|
let textLayerInstance: { cancel: () => void } | null = null;
|
||||||
|
|
||||||
|
// Holds the dynamically-loaded pdfjs module (browser-only)
|
||||||
|
// Not $state — we use pdfjsReady as the reactive trigger instead
|
||||||
|
let pdfjsLib: typeof import('pdfjs-dist') | null = null;
|
||||||
|
let pdfjsReady = $state(false);
|
||||||
|
|
||||||
|
onMount(async () => {
|
||||||
|
// Dynamic import keeps pdfjs out of the SSR bundle entirely
|
||||||
|
const [lib, { default: workerUrl }] = await Promise.all([
|
||||||
|
import('pdfjs-dist'),
|
||||||
|
import('pdfjs-dist/build/pdf.worker.min.mjs?url')
|
||||||
|
]);
|
||||||
|
lib.GlobalWorkerOptions.workerSrc = workerUrl;
|
||||||
|
pdfjsLib = lib;
|
||||||
|
pdfjsReady = true;
|
||||||
|
});
|
||||||
|
|
||||||
async function loadDocument(src: string) {
|
async function loadDocument(src: string) {
|
||||||
|
if (!pdfjsLib) return;
|
||||||
loading = true;
|
loading = true;
|
||||||
error = null;
|
error = null;
|
||||||
pdfDoc = null;
|
pdfDoc = null;
|
||||||
@@ -30,7 +44,7 @@ async function loadDocument(src: string) {
|
|||||||
totalPages = 0;
|
totalPages = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const loadingTask = getDocument(src);
|
const loadingTask = pdfjsLib.getDocument(src);
|
||||||
const doc = await loadingTask.promise;
|
const doc = await loadingTask.promise;
|
||||||
pdfDoc = doc;
|
pdfDoc = doc;
|
||||||
totalPages = doc.numPages;
|
totalPages = doc.numPages;
|
||||||
@@ -42,7 +56,7 @@ async function loadDocument(src: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function renderPage(doc: PDFDocumentProxy, pageNum: number) {
|
async function renderPage(doc: PDFDocumentProxy, pageNum: number) {
|
||||||
if (!canvasEl || !textLayerEl) return;
|
if (!pdfjsLib || !canvasEl || !textLayerEl) return;
|
||||||
|
|
||||||
// Cancel any in-flight render
|
// Cancel any in-flight render
|
||||||
if (renderTask) {
|
if (renderTask) {
|
||||||
@@ -79,8 +93,6 @@ async function renderPage(doc: PDFDocumentProxy, pageNum: number) {
|
|||||||
try {
|
try {
|
||||||
await task.promise;
|
await task.promise;
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
// Cancelled renders throw a specific error — ignore them
|
|
||||||
if (e instanceof Error && e.message === 'Rendering cancelled') return;
|
|
||||||
if (
|
if (
|
||||||
typeof e === 'object' &&
|
typeof e === 'object' &&
|
||||||
e !== null &&
|
e !== null &&
|
||||||
@@ -98,7 +110,7 @@ async function renderPage(doc: PDFDocumentProxy, pageNum: number) {
|
|||||||
textDiv.style.width = `${viewport.width / dpr}px`;
|
textDiv.style.width = `${viewport.width / dpr}px`;
|
||||||
textDiv.style.height = `${viewport.height / dpr}px`;
|
textDiv.style.height = `${viewport.height / dpr}px`;
|
||||||
|
|
||||||
const tl = new TextLayer({
|
const tl = new pdfjsLib.TextLayer({
|
||||||
textContentSource: page.streamTextContent(),
|
textContentSource: page.streamTextContent(),
|
||||||
container: textDiv,
|
container: textDiv,
|
||||||
viewport
|
viewport
|
||||||
@@ -123,7 +135,7 @@ async function prerender(doc: PDFDocumentProxy, pageNum: number) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
if (url) {
|
if (pdfjsReady && url) {
|
||||||
loadDocument(url);
|
loadDocument(url);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { m } from '$lib/paraglide/messages.js';
|
|||||||
import { formatDate } from '$lib/utils/date';
|
import { formatDate } from '$lib/utils/date';
|
||||||
import { diffWords } from 'diff';
|
import { diffWords } from 'diff';
|
||||||
import ExpandableText from '$lib/components/ExpandableText.svelte';
|
import ExpandableText from '$lib/components/ExpandableText.svelte';
|
||||||
import PdfViewer from '$lib/components/PdfViewer.svelte';
|
|
||||||
|
|
||||||
let { data } = $props();
|
let { data } = $props();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user