-
-
Notifications
You must be signed in to change notification settings - Fork 906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Have a 504 error when getting pdf.worker.js
using vite entry
#1377
Comments
Having the same issue Couldn't get it to work. |
Tried using it with vite 4 and got the same error. 😞 |
When I set an
Looking in the N.B. I've not included |
请问解决了吗? |
Don't use import { Document, Page } from 'react-pdf';
import * as pdfjs from 'pdfjs-dist/build/pdf';
pdfjs.GlobalWorkerOptions.workerSrc = new URL(
'pdfjs-dist/build/pdf.worker.js',
import.meta.url
); |
Indeed it appears that because of vitejs/vite#10839 Vite entry file does not behave correctly. If you guys have any idea how we could possibly implement this to make it work out of the box again, I'd be very grateful. |
Thanks @idaibin , while that didn't quite work for me it did lead me to implement one of the other import { Document, Page, pdfjs } from 'react-pdf';
pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.js`; which works both when running in I suspect that the |
@idaibin thanks, it works for me. First, my My final code using typescript: import { useState } from 'react'
import { Document, Page } from 'react-pdf'
import * as pdfjs from 'pdfjs-dist'
import type { DocumentProps } from 'react-pdf'
const src = new URL('pdfjs-dist/build/pdf.worker.js', import.meta.url)
pdfjs.GlobalWorkerOptions.workerSrc = src.toString() |
@Gu-Miao your solution is now actually the recommended one in v7 😅 See README for details. |
@Gu-Miao did you manage to make it work without having to explicitly add So far I've tried to add |
If you're using vite you can import the worker url (no need to add it as a dependency, since This solution is actually the same shown in documentation, the only difference is that vite will handle getting the right url path, which may be is necessary to work in different environments. import { pdfjs } from 'react-pdf'
import pdfWorkerURL from 'pdfjs-dist/build/pdf.worker.min?url'
pdfjs.GlobalWorkerOptions.workerSrc = new URL(pdfWorkerURL, import.meta.url).toString() |
This is working fine in dev mode but in the build preview using vite, its not. Any idea on that ? @athenacfr |
This worked for me (React/Vite):
Sources: Importing Script as a Worker, vite.dev, Have a 504 error when getting pdf.worker.js using vite entry 1377, |
Still having trouble getting this worker script loading properly in my React app, using Vite. Using the recommended import solution for the worker, and also trying the solution that @anton-x-t suggested, resulted in this error: Vite: 4.5.5 Only solution that works for me is grabbing the worker from a CDN, which I guess is fine for now. |
It seems your server is not handling mjs/cjs a extensions properly, I had the same problem. If you use nginx you this should work:
|
OH MY GOD!!! Thank you so much! I've tried all the possible solutions listed in this and related threads and this was the solution!!! I'm using react-pdf 9.1.1 and vite 6.0.2 and using the documented approach everything worked well when developing but breaks when building and deploying it via docker on an nginx server with the same problem relative to handling mjs as octect-stream. For other people reference: PDFPage.jsx
Dockerfile
mime.types
|
Before you start - checklist
Description
I create a simple demo, but it can't work. I use pnpm, vite@4, typescript.
My code:
I get a 504 error when getting the
pdf.worker.js
:In the
.vite
dir, the bundle is:I also tried this: #1148 (comment). But it's not good for me.
By the way, the entry path in README is outdate. It's not
index.vite
, it'sentry.vite
now.Steps to reproduce
A minimal reproduction is here: https://github.com/Gu-Miao/react-pdf-test
Expected behavior
Demo works fun
Actual behavior
Get a 504 error when getting
pdf.worker.js
Additional information
No response
Environment
The text was updated successfully, but these errors were encountered: