-
-
Notifications
You must be signed in to change notification settings - Fork 901
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
react-pdf is not working when build with nextjs #136
Comments
Hello @nyamba, You can manually set workerSrc path to whatever path you like, so if you define where the worker file should be copied to (hint 2: it does not need building, it can be safely just copied from pdfjs-dist node_module) and how it's called, then your problem is solved entirely. You could also do the opposite thing, meaning - looking where React-PDF looks for worker file and ensure your bundling process puts it there. But the first option is a safer bet :) |
FYI for others using Next.js they have a helper that we used to work around this issue. Our problem was that rendering anything which depends on PDF.js server-side will throw errors, as the library itself contains browser API calls which are not safe in Node. By using the above helper to import the component that handles PDF rendering we were able to defer parsing of the library code to the browser, where it ran without error: // This is our wrapper component which contains the PDF viewer
const PDFViewer = dynamic(import('../../components/PDFViewer'), { ssr: false });
class ExampleClass extends Component {
render() {
return (
<div>
<PDFViewer />
</div>
);
}
} |
I was facing this problem for a long time in a nextJs production environment: __next[hash].worker.js not found on custom server. Both provided solutions with custom setOptions method or dynamic import didn't work for me.
I don't know if it's recommended, but it now works in production. |
I see nothing wrong with this code, apart from the fact it won't work with React-PDF 4.x, since there's no longer an option to run React-PDF without a Worker. All browsers should be capable of running the worker though, so you should aim to enable it if possible. |
I am also using next.js and have the same issue, I tried many suggestions and workarounds but failed. I ended up using this module without |
I cannot reproduce the issue with:
This works just fine for me: import React from "react";
import { Document, Page } from "react-pdf";
class Test extends React.Component {
render() {
return (
<Document
file={{
url: "./my.pdf"
}}
onLoadError={e =>
console.log("Error while loading document! " + e.message)
}
onSourceError={e =>
console.log("Error while loading document! " + e.message)
}
>
<Page pageNumber={1} />
</Document>
);
}
}
const index = () => {
return (
<div>
<Test></Test>
</div>
);
};
export default index; |
the issue happens when trying to use the service worker implementation, which is supposed to be imported like this: somewhat related... @wojtekmaj , I'm not seeing the |
@RyanDriscoll You're looking at docs for the newer version of React-PDF than you're currently using. Read the docs for 4.1.0 here: https://github.com/wojtekmaj/react-pdf/tree/v4.1.0 |
Have tried this on Next v9 stack and React-PDF v4.1 alongside @felizuno's Dynamic Import patch.
However, the PDF gets stuck in loading forever. How to get the webpack working with NextJS? Have also tried to install Next Workers to no avail. (Still loading) |
You're looking at React-PDF 5.0 docs. |
This comment has been minimized.
This comment has been minimized.
In case anyone comes here looking for NextJs implementation...I got it to work just fine with the below (an amalgamation of all the comments above; thanks all): Install as per documentation: NB: ensure you don't already have pdfjs-dist installed, as the version you installed may mismatch this package. Create a wrapped component, as per documentation:
Import dynamically, as SSR FALSE:
This will ensure that the correct version of pdf.js is pulled in from this react-pdf package, and Next/webpack does the rest. |
@redgumnfp this is no longer working, error returned is
Resulting from a recent-ish change in webpack. Pulling from the CDN seems to work however.
|
I have created a minimal next-js + react-pdf codesandbox and everything works fine with current version, look |
@jeetiss Thanks! Actually, I think you don't even need to import it dynamically when using it this way. |
@paescuj yes you right! react-pdf works fine with webpack 5, but it still is "future" feature, so I keep dynamic import with webpack 4 react-pdf throws some warnings about window and file access |
Hey guys, import { pdfjs } from 'react-pdf';
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.min.js`; |
…maj/react-pdf#136 result compiled successfully
@adderpositive thanks for that! Solved loading issues when deploying to Vercel. Not my favorite thing to do but when in a pinch. |
Unfortunately, these solutions fail with To reproduce the issue, use https://github.com/phbernard/nextjs-react-pdf-sandbox . This sandbox has been forked from @jeetiss's one above (thank you by the way, great resource!), with a change in
With this project,
I managed to make it work using webpack externals, still in
Now the project builds and works as expected. However, I get warnings at build time such as:
This might be harmless, see vercel/next.js#10633 . I consider this solution as work-in-progress and don't advise to follow it blindly. I put it here because I couldn't find this anywhere. |
If I try to import components from
It's appear even if I add Now I'm using worker file from CDN to resolve this |
@jeetiss Thanks a lot for your example! |
@phbernard I agree, I have tried those solutions but it seems it will still fail with
And when I tried to solve the
And lastly, when I tried to put it inside externals the build will give a result error memory heap. I also have tried to only add externals by "pushing" the canvas with |
@asharimh97 did you solve this issues by any chance? |
I was able to get this working without needing to manually copy the pdf worker file into the public folder by using the copy-webpack-plugin. This will automatically copy the pdf worker file into your public folder when the application builds. Add something like this to your custom webpack config: config.plugins.push(
new CopyPlugin({
patterns: [
{
from: path.join(__dirname, 'node_modules/pdfjs-dist/build/pdf.worker.min.js'),
to: path.join(__dirname, 'public'),
},
],
})
); And update the pdfjs.GlobalWorkerOptions.workerSrc = '/pdf.worker.min.js'; |
@arnars @asharimh97 @phbernard I was able to successfully deploy to Netlify using this webpack solution: #799 (comment). Hope that helps! |
@jeetiss - Your shared solution stopped the pain for me. Thanks |
I used the same method with |
with nextjs 12.0.4 this solution worked perfectly for me, and i dont even have to use the dynamic import |
$ npm i -D copy-webpack-plugin // next.config.js
const path = require('path')
const CopyPlugin = require("copy-webpack-plugin");
const pdfWorkerPath = require.resolve(
`pdfjs-dist/build/pdf.worker${
process.env.NODE_ENV === "development" ? ".min" : ""
}.js`
);
/** @type {import('next').NextConfig} */
const nextConfig = {
// ...
webpack: (config) => {
config.plugins.push(
new CopyPlugin({
patterns: [
{
from: pdfWorkerPath,
to: path.join(__dirname, 'public'),
},
],
})
);
return config;
},
};
module.exports = nextConfig; |
Thank you, this works for me! |
This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this issue will be closed in 14 days. |
This issue was closed because it has been stalled for 14 days with no activity. |
@abernier |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
bro @jeetiss, how do you deploy this solution on vercel?? it's giving me build error -
any help anyone? |
@ghostneneji did it get deployed? I'm facing issue in deployment on vercel |
It's working fine on development mode with nextjs. However when I build my nextjs app, it gives me an error that worker.js is not found(404). It looks that *.worker.js is existed in .next director.
Can you point me to a way to resolve it.
Thanks for your time.
The text was updated successfully, but these errors were encountered: