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.

window is not defined in SSR

See original GitHub issue

While 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
wojtekmajcommented, Mar 19, 2021

but which particular files/folders are you using?

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.

1reaction
Snuffleupaguscommented, Mar 18, 2021

Another way to fix it would be to simply write

const animationStarted = new Promise(function (resolve) {
  if (typeof window === "undefined" || typeof window.requestAnimationFrame !== "function")) {
    setTimeout(resolve, 20);
  } else {
    window.requestAnimationFrame(resolve);
  }
});

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.

Read more comments on GitHub >

github_iconTop 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 >

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