mirror of
https://github.com/lobehub/lobehub.git
synced 2026-03-27 13:29:15 +07:00
🐛 fix: load PDF.js worker from local assets via Vite ?url import (#13006)
This commit is contained in:
@@ -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} />;
|
||||
};
|
||||
|
||||
|
||||
5
src/vite.d.ts
vendored
5
src/vite.d.ts
vendored
@@ -1,3 +1,8 @@
|
||||
import 'vite/client';
|
||||
|
||||
declare module 'pdfjs-dist/build/pdf.worker.min.mjs?url' {
|
||||
const url: string;
|
||||
export default url;
|
||||
}
|
||||
|
||||
export {};
|
||||
|
||||
Reference in New Issue
Block a user