question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

worker issue with Gatsby 4 / webpack 5

See original GitHub issue

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’m using Gatsby 4 (with webpack 5) and it works fine in develop mode. However in production mode (gatsby build; gatsby serve) I get the following error in the console:

GET http://localhost:9001/[object%20Object] 404 (Not Found)

The PDF fails to render and instead I get an error message Failed to load PDF file.

Steps to reproduce

  1. clone https://github.com/jakoblind/gatsby-react-pdf-bug-repro
  2. npm install
  3. npx gatsby build
  4. npx gatsby serve
  5. go to http://localhost:9000/
  6. Se error as described above.

Expected behavior

To se a rendered PDF

Actual behavior

The PDF fails to render and instead I get an error message Failed to load PDF file.

Additional information

No response

Environment

  • React-PDF version: 5.7.2
  • React version: 17.0.1
  • Webpack version (if applicable): 5

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
jakoblindcommented, Jun 1, 2022

@egemenu Try this

if (typeof window !== 'undefined' && 'Worker' in window) {
  pdfjs.GlobalWorkerOptions.workerPort = new Worker(
    new URL('pdfjs-dist/legacy/build/pdf.worker', import.meta.url)
  )
}

Does that work for you without CopyWebpackPlugin? it does for me.

2reactions
alanpilloudcommented, May 29, 2022

Hi @jakoblind,

I solved the same issue by loading the worker this way:

import { pdfjs } from "react-pdf"
pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.js`

I found these instructions here even though it is not related to webpack: https://github.com/wojtekmaj/react-pdf#standard-browserify-esbuild-and-others

It worked with gatsby serve and on Netlify.

Hope it solves your issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack v5 polyfill error with Gatsby site - Stack Overflow
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default. This is no longer the case.
Read more >
Troubleshooting Common Errors - Gatsby
If you encounter a webpack error that says Generating SSR bundle failed after installing a plugin and trying to run gatsby develop or...
Read more >
To v5 from v4 - webpack
This guide aims to help you migrating to webpack 5 when using webpack directly. If you are using a higher level tool to...
Read more >
Barebones Web Workers - Bayan Bennett
Barebones Web Workers. Gatsby JS. Webpack. JavaScript. Once there is enough of a reason to offload calculations into another thread, WebWorkers begin to ......
Read more >
Use Web Workers in a Gatsby project - DEV Community ‍ ‍
Tagged with gatsby, javascript, webpack. ... approach in a GitHub issue, and decided to write it up here for anyone ... doExpensiveTask(5) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found