React-pdf: When using Server Side Rendering, an error message "ReferenceError: window is not defined" appears

Created on 5 Dec 2017  路  9Comments  路  Source: wojtekmaj/react-pdf

Hello,

Maybe I missed something but if someone can help me it will be great.
When I import Document and Page on my .jsx file, webpack crash on launch.
(cf: import { Document, Page } from 'react-pdf')

But when I remove the import, webpack launch without problem.

The fact is my project is running with server side rendering, and I think that the attribute window is not checked inside Document and Page component (cf : if typeof window !== 'undefined')

Thank you for listening my issue.

bug

Most helpful comment

It's an issue on PDF.js side, but I see it has been fixed. Check out newest v4.0.0-beta.3 - it should work!

Should have been fixed in mozilla/pdf.js#9769

All 9 comments

Hey,
would you please include an error stack if you have any?

Hey @Ilyomix,
do you still have this issue? If so, please share more details! I'd really like to get that fixed :)

Fixed in v2.5.1. Hopefully. ;)
Let me know in case I'm wrong.
Thanks!

Hey @wojtekmaj, thanks for this great package :) I'm having this same issue with SSR on version 3.0.5, did you fix this in 2.5.1?

Hello, this is unlikely the same issue. Has to be another similar one.

Would you please give me the full error stack?

Also please mind that this library makes little sense on Server Side. Canvas has to be rendered on client side.

UnhandledPromiseRejectionWarning: ReferenceError: window is not defined
at /usr/src/app/node_modules/pdfjs-dist/lib/web/ui_utils.js:391:3
at new Promise ()
at Object. (/usr/src/app/node_modules/pdfjs-dist/lib/web/ui_utils.js:390:24)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Module.require (module.js:604:17)
at require (internal/module.js:11:18)

I have, however, circumvented the error by checking that the code is running in a browser before rendering the component.

@wojtekmaj This is my full error stack.

UnhandledPromiseRejectionWarning: ReferenceError: window is not defined
at /root/morphling/node_modules/pdfjs-dist/lib/web/ui_utils.js:391:3
at new Promise (<anonymous>)
at Object.<anonymous> (/root/morphling/node_modules/pdfjs-dist/lib/web/ui_utils.js:390:24)
at Module._compile (module.js:652:30)
at Module._extensions..js (module.js:663:10)
at Object.require.extensions.(anonymous function) [as .js] (/root/morphling/node_modules/babel-register/lib/node.js:152:7)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)

It's an issue on PDF.js side, but I see it has been fixed. Check out newest v4.0.0-beta.3 - it should work!

Should have been fixed in mozilla/pdf.js#9769

10 months later (I moved to Gitlab for work), thank you @wojtekmaj ;) !!

Was this page helpful?
0 / 5 - 0 ratings