-
-
Notifications
You must be signed in to change notification settings - Fork 893
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
Production build fails on Next.js #1824
Comments
I have the same issue. I'm using the |
Actual error on non-minified worker file:
|
Related: I found that
fixes the issue, but disables minification, which is not cool.
also fixes the issue, but causes the build to be slightly slower. I also found that the message is probably not coming from Terser, but SWC: https://github.com/swc-project/swc/blob/abcf21dfc27b61489b614ce4ec0ae6733700578f/crates/swc_ecma_parser/src/error.rs#L415 |
Since it's a bug in Next.js not React-PDF, I'm closing this issue after updating upgrade guide wiki. Please kindly share your further feedback on importing ESM workers with Next.js team. |
Using an external CDN also works around this issue import {pdfjs} from 'react-pdf'
-pdfjs.GlobalWorkerOptions.workerSrc = new URL(
- 'pdfjs-dist/build/pdf.worker.min.mjs',
- import.meta.url,
-).toString();
+pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.mjs`;
|
@0livare Thank you! |
This was a head-scratcher to track down. I was trying to switch from using the CDN to hosting myself but hit this webpack issue. I went back to CDN but Firefox shows CORS errors. Finally figured out that adding "https" fixes the CORS issue and this seems to work in all browsers I've tested:
|
I solved it by using this directly: // In a component that needs to use this
import 'pdfjs-dist/build/pdf.worker.min.mjs';
// And delete this or some example implementation shown in the doc:
pdfjs.GlobalWorkerOptions.workerSrc = new URL(
'pdfjs-dist/build/pdf.worker.min.mjs',
import.meta.url,
).toString(); Additionally, if you have errors in the tests as a result of this import, you can do the following: // jest.config.ts
{
...
moduleNameMapper:{
'pdfjs-dist/build/pdf.worker.min.mjs': '<rootDir>/src/test/__mocks__/mock-pdf-worker.ts'
} ,
transformIgnorePatterns: ['/node_modules/(?!pdfjs-dist/)'],
} // Create the mock-pdf-worker.ts file and it must match the path that was placed in jest.config.ts
const mockPdfWorker = {};
export default mockPdfWorker; |
thanks |
Thanks you, @legancode // In a component that needs to use this
import 'pdfjs-dist/build/pdf.worker.min.mjs';
// And delete this or some example implementation shown in the doc:
// pdfjs.GlobalWorkerOptions.workerSrc = new URL(
// ' pdfjs-dist/build/pdf.worker.min.mjs',
// import.meta.url,
// ).toString(); |
Before you start - checklist
Description
Running
yarn build
fails on/sample/next-app
(and all other Next apps)Steps to reproduce
next-app
sampleyarn build
Expected behavior
Production builds work
Actual behavior
Build throws a Webpack error
Additional information
It's working on 8.x.x so likely something to do with the
.mjs
extension of the workerEnvironment
The text was updated successfully, but these errors were encountered: