fix(astrolabe): load pdfjs-dist externally to fix PDF viewer

When viewing PDF chunks in semantic search, the PDF viewer failed with
"can't access private field" errors. This was caused by:

1. CSP blocks web workers (worker-src 'none'), forcing fake worker mode
2. Vite transforms ES private fields in the bundle, but the worker file
   is untransformed, causing incompatible private field implementations
3. Vue's ref() wraps PDFDocumentProxy in a Proxy, which can't access
   ES private fields

Fixed by:
- Loading pdfjs-dist externally via script tag (avoids Vite transform)
- Creating pdfjs-loader.mjs that imports pdf.mjs and sets window.pdfjsLib
- Using Util::addScript() for CSP-compliant script loading with nonces
- Using shallowRef() instead of ref() for pdfDoc to avoid Proxy wrapper
- Setting workerSrc at runtime using OC.linkTo() for correct app path

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Chris Coutinho
2026-01-25 21:08:44 +01:00
parent c76dd21eeb
commit 38adb96be4
4 changed files with 54 additions and 19 deletions
+7 -2
View File
@@ -2,10 +2,15 @@
declare(strict_types=1);
use OCA\Astrolabe\AppInfo\Application;
use OCP\Util;
Util::addScript(OCA\Astrolabe\AppInfo\Application::APP_ID, OCA\Astrolabe\AppInfo\Application::APP_ID . '-main');
Util::addStyle(OCA\Astrolabe\AppInfo\Application::APP_ID, OCA\Astrolabe\AppInfo\Application::APP_ID . '-main');
// Load PDF.js loader first (must be external, not bundled by Vite,
// to avoid ES private field transformation issues with fake worker fallback)
// The loader imports pdf.mjs and sets window.pdfjsLib before the main app runs
Util::addScript(Application::APP_ID, 'pdfjs-loader');
Util::addScript(Application::APP_ID, Application::APP_ID . '-main');
Util::addStyle(Application::APP_ID, Application::APP_ID . '-main');
?>