window is not defined in SSR
See original GitHub issueWhile using pdfjs in frameworks with SSR like Next.js or Gatsby there is an error while build phase
ReferenceError: window is not defined
The error occurs at line 563 in ui_utils.js
Looks like all changes needs is:
const animationStarted = new Promise(function (resolve) {
if (
typeof PDFJSDev !== "undefined" &&
- PDFJSDev.test("LIB && TESTING") &&
+ PDFJSDev.test("LIB && TESTING") ||
typeof window === "undefined"
) {
// Prevent "ReferenceError: window is not defined" errors when running the
// unit-tests in Node.js/Travis.
setTimeout(resolve, 20);
return;
}
window.requestAnimationFrame(resolve);
});
There is an issue in react-pdf: https://github.com/wojtekmaj/react-pdf/issues/657
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:11 (3 by maintainers)
Top Results From Across the Web
React JS Server side issue - window not found - Stack Overflow
So I use process.env.BROWSER to my advantage because it will be defined as undefined if it is server side, and it will be...
Read more >Using window in React SSR: The Complete Guide
window is not defined on the server, so you can't use it during the render of a component being SSR'd. ... During a...
Read more >How to resolve window is not defined on npm run serve:ssr ...
The issue is caused by package webpack, I think but not sure. Description. The command line : "npm run serve:ssr" occurred the exception...
Read more >Angular Universal: ReferenceError: window is not defined
This error can be caused by a reference to the Window object if you are rendering your application from a server like Node.js....
Read more >referenceerror: window is not defined, how to solve
Here's how to fix the “referenceerror: window is not defined” error that you might have in Node.js or with a tool like Next.js....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think the issue here was that I needed PdfLinkService to get links working in PDFs, and PDFLinkService required EventBus, which, when imported, evaluated the entire ui_utils.js unfortunately, because ui_utils isn’t side effect free. I created our own custom link service now in React-PDPF 5.3.0 beta so that should no longer be an issue - at least from our side, unless someone else will try the same thing.
That’s not really an acceptable solution though, since as a general rule we don’t want to add useless runtime checks in Firefox; please note https://github.com/mozilla/pdf.js/issues/13057#issuecomment-792249114.