From 38adb96be4e8abdf9c896da002b8a5bce244cfe5 Mon Sep 17 00:00:00 2001 From: Chris Coutinho Date: Sun, 25 Jan 2026 21:08:44 +0100 Subject: [PATCH] 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 --- third_party/astrolabe/src/App.vue | 12 ------ .../astrolabe/src/components/PDFViewer.vue | 15 ++++++-- third_party/astrolabe/templates/index.php | 9 ++++- third_party/astrolabe/vite.config.js | 37 ++++++++++++++++++- 4 files changed, 54 insertions(+), 19 deletions(-) diff --git a/third_party/astrolabe/src/App.vue b/third_party/astrolabe/src/App.vue index 7c646bf..6886cd9 100644 --- a/third_party/astrolabe/src/App.vue +++ b/third_party/astrolabe/src/App.vue @@ -394,18 +394,6 @@ import MarkdownViewer from './components/MarkdownViewer.vue' import axios from '@nextcloud/axios' import { generateUrl } from '@nextcloud/router' import Plotly from 'plotly.js-dist-min' -import * as pdfjsLib from 'pdfjs-dist' - -// Set worker source with error handling -try { - pdfjsLib.GlobalWorkerOptions.workerSrc = new URL( - 'pdfjs-dist/build/pdf.worker.mjs', - import.meta.url, - ).toString() -} catch (e) { - console.warn('Failed to set PDF.js worker, will use fallback', e) - // PDF.js will use fake worker automatically -} export default { name: 'App', diff --git a/third_party/astrolabe/src/components/PDFViewer.vue b/third_party/astrolabe/src/components/PDFViewer.vue index 60d6611..ac81f91 100644 --- a/third_party/astrolabe/src/components/PDFViewer.vue +++ b/third_party/astrolabe/src/components/PDFViewer.vue @@ -15,11 +15,14 @@