From 3a4aabff1d539cf6623df75148c53b659a0c7e0a Mon Sep 17 00:00:00 2001 From: vikingowl Date: Sat, 3 Jan 2026 22:16:19 +0100 Subject: [PATCH] fix: bundle PDF.js worker locally to fix CDN loading issues - Add postinstall script to copy worker to static/ - Update Dockerfile to copy worker during build - Update file-processor to try local worker first, fallback to CDN - Bump version to 0.4.11 --- .gitignore | 3 +++ backend/cmd/server/main.go | 2 +- frontend/Dockerfile | 4 ++++ frontend/package.json | 5 +++-- frontend/src/lib/utils/file-processor.ts | 14 ++++++++++++-- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 572b1c3..461a096 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,6 @@ dev.env backend/vessel-backend data/ backend/data-dev/ + +# Generated files +frontend/static/pdf.worker.min.mjs diff --git a/backend/cmd/server/main.go b/backend/cmd/server/main.go index 7cc26ff..5a31e0e 100644 --- a/backend/cmd/server/main.go +++ b/backend/cmd/server/main.go @@ -18,7 +18,7 @@ import ( ) // Version is set at build time via -ldflags, or defaults to dev -var Version = "0.4.10" +var Version = "0.4.11" func getEnvOrDefault(key, defaultValue string) string { if value := os.Getenv(key); value != "" { diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 7885081..76323a7 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -12,6 +12,10 @@ RUN npm ci # Copy source code COPY . . +# Copy PDF.js worker to static directory for local serving +# This avoids CDN dependency and CORS issues with ESM modules +RUN cp node_modules/pdfjs-dist/build/pdf.worker.min.mjs static/ + # Build the application RUN npm run build diff --git a/frontend/package.json b/frontend/package.json index 3d5735e..62ff96a 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,6 +1,6 @@ { "name": "vessel", - "version": "0.4.10", + "version": "0.4.11", "private": true, "type": "module", "scripts": { @@ -11,7 +11,8 @@ "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch", "test": "vitest run", "test:watch": "vitest", - "test:coverage": "vitest run --coverage" + "test:coverage": "vitest run --coverage", + "postinstall": "cp node_modules/pdfjs-dist/build/pdf.worker.min.mjs static/ 2>/dev/null || true" }, "devDependencies": { "@sveltejs/adapter-auto": "^4.0.0", diff --git a/frontend/src/lib/utils/file-processor.ts b/frontend/src/lib/utils/file-processor.ts index 4e3078b..7f969a6 100644 --- a/frontend/src/lib/utils/file-processor.ts +++ b/frontend/src/lib/utils/file-processor.ts @@ -150,8 +150,18 @@ async function loadPdfJs(): Promise { try { pdfjsLib = await import('pdfjs-dist'); - // Set worker source using CDN for reliability - pdfjsLib.GlobalWorkerOptions.workerSrc = `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjsLib.version}/pdf.worker.min.mjs`; + // Use locally bundled worker (copied to static/ during build) + // Falls back to CDN if local worker isn't available + const localWorkerPath = '/pdf.worker.min.mjs'; + const cdnWorkerPath = `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjsLib.version}/pdf.worker.min.mjs`; + + // Try local first, with CDN fallback + try { + const response = await fetch(localWorkerPath, { method: 'HEAD' }); + pdfjsLib.GlobalWorkerOptions.workerSrc = response.ok ? localWorkerPath : cdnWorkerPath; + } catch { + pdfjsLib.GlobalWorkerOptions.workerSrc = cdnWorkerPath; + } return pdfjsLib; } catch (error) {