Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: load PDF.js worker from local assets via Vite ?url import (#13006)

Xial46455cb6c35f
diff --git a/src/libs/pdfjs/index.tsx b/src/libs/pdfjs/index.tsx
index 9e4fd3e38b6..2182f8273ef 100644
--- a/src/libs/pdfjs/index.tsx
+++ b/src/libs/pdfjs/index.tsx
@@ -4,19 +4,16 @@ import { type ComponentProps } from 'react';
import { type Page as PdfPage } from 'react-pdf';
import { Document as PdfDocument, pdfjs } from 'react-pdf';
-const workerSrc = `https://registry.npmmirror.com/pdfjs-dist/${pdfjs.version}/files/build/pdf.worker.min.mjs`;
+// Use Vite's ?url import to get the correct hashed asset path (e.g. /spa/assets/pdf.worker-xxx.mjs)
+// This overrides react-pdf's auto-detected bare filename which breaks under SPA routing.
+import pdfjsWorkerUrl from 'pdfjs-dist/build/pdf.worker.min.mjs?url';
-function ensureWorker() {
- if (!pdfjs.GlobalWorkerOptions.workerSrc) {
- pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
- }
-}
+pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorkerUrl;
export type DocumentProps = ComponentProps<typeof PdfDocument>;
export type PageProps = ComponentProps<typeof PdfPage>;
export const Document = (props: DocumentProps) => {
- ensureWorker();
return <PdfDocument {...props} />;
};
diff --git a/src/vite.d.ts b/src/vite.d.ts
index b4868d5148e..821c6319fe6 100644
--- a/src/vite.d.ts
+++ b/src/vite.d.ts
@@ -1,3 +1,8 @@
import 'vite/client';
+declare module 'pdfjs-dist/build/pdf.worker.min.mjs?url' {
+ const url: string;
+ export default url;
+}
+
export {};