react-pdf: Have a 504 error when getting `pdf.worker.js` using vite entry
Before you start - checklist
- I followed instructions in documentation written for my React-PDF version
- I have checked if this bug is not already reported
- I have checked if an issue is not listed in Known issues
- If I have a problem with PDF rendering, I checked if my PDF renders properly in PDF.js demo
Description
I create a simple demo, but it can’t work. I use pnpm, vite@4, typescript.
My code:
import { useState } from 'react'
import { Document, Page } from 'react-pdf/dist/esm/entry.vite'
import type { DocumentProps } from 'react-pdf'
function App() {
const [numPages, setNumPages] = useState(0)
const [pageNumber] = useState(1)
const onDocumentLoadSuccess: DocumentProps['onLoadSuccess'] = ({ numPages }) => {
setNumPages(numPages)
}
return (
<div>
<Document file="/test.pdf" onLoadSuccess={onDocumentLoadSuccess}>
<Page pageNumber={pageNumber} />
</Document>
<p>
Page {pageNumber} of {numPages}
</p>
</div>
)
}
export default App
I get a 504 error when getting the pdf.worker.js:

In the .vite dir, the bundle is:

I also tried this: https://github.com/wojtekmaj/react-pdf/issues/1148#issuecomment-1314180559. But it’s not good for me.
By the way, the entry path in README is outdate. It’s not index.vite, it’s entry.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
- Browser (if applicable): Chrome 110
- React-PDF version: 6.2.2
- React version: 18.2.0
- Vite version: 4.1.4
- Pnpm version: 7.29.1
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 5
- Comments: 16 (2 by maintainers)
@idaibin thanks, it works for me.
First, my
react-pdf’s version is6.2.2, so I need apdfjs-dist@2.16.105in mydependencies. You can check it inpackage.jsonof yourreact-pdfpackage innode_modulesdirectory.My final code using typescript:
Don’t use
entry.vite, it works.This worked for me (React/Vite):
Sources: What should be the value for workerSrc? 8305, thank you Leung Yau Ming, https://github.com/mozilla/pdf.js/issues/8305#issuecomment-1674973556
Importing Script as a Worker, vite.dev, thank you helloworld3200 et al,
https://vite.dev/guide/assets.html#importing-script-as-a-worker
Have a 504 error when getting pdf.worker.js using vite entry 1377, thank you Athena Freitas, https://github.com/wojtekmaj/react-pdf/issues/1377#issuecomment-2374304234
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:
If you’re using vite you can import the worker url (no need to add it as a dependency, since
react-pdfalready has it as its dependency).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.
When I set an
onLoadErrorevent handler to display theerror.messagein a Vite setup (using the suggestedentry.vitefrom the README.- noteentryand notindex.vite), I see the following returned:Looking in the
node_modules/.vite/depsdirectory, there indeed isn’t apdfjs-dist/sub-directory (nevermind the rest). There is anode_modules/.vite/deps/react-pdf_dist_esm_entry__vite.jsfile (along with the same file with a.mapsextension) - is this what should be referred to byreact-pdfinstead or is the requiredpdf.worker.jsnot being included in the.vite/depsas expected? In which case, where should it come from and how should it get here?N.B. I’ve not included
pdfjs-distas an explicit dependency withinpackage.jsonasreact-pdfis using an old version (2.xrather than the current3.x) and keeping the two inline could be challening in future.Having the same issue Tried to lower version to 6.0.2 as to when it was solved. https://github.com/wojtekmaj/react-pdf/issues/1148#issuecomment-1314180559
Couldn’t get it to work.