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.

[Feature Request] Load as an import without exception being thrown when using Server Side Rendering

See original GitHub issue

Is your feature request related to a problem? Please describe.

I’m using DarkReader API inside a Svelte/Sapper project.

This technology stack enables SSR of a TS page before serving to the browser.

Because node javascript runtime for SSR does not contain window, or window.chrome (or possibly others), node throws an error when importing the darkreader package.

This error is not recoverable because darkreader cannot be conditionally included (I’m in typescript), and the entire page and it’s imports are required to be loaded.

The following is thrown when trying to run a component for Server Side Rendering.

ReferenceError: window is not defined 

This can be verified by running the following

$ node node_modules/darkreader/darkreader.js

Which will result in

/xxx/node_modules/.pnpm/darkreader@4.9.26/node_modules/darkreader/darkreader.js:4203
    throw e;
    ^

ReferenceError: window is not defined
    at /xxx/node_modules/.pnpm/darkreader@4.9.26/node_modules/darkreader/darkreader.js:244:5
    at /xxx/node_modules/.pnpm/darkreader@4.9.26/node_modules/darkreader/darkreader.js:7:68
    at Object.<anonymous> (/xxx/node_modules/.pnpm/darkreader@4.9.26/node_modules/darkreader/darkreader.js:10:2)

Describe the solution you’d like

Ensure there are no “assumed property is defined” checks inside DarkReader when package is imported.

For example

if (!window.chrome) {
    window.chrome = {} as any;
}
if (!chrome.runtime) {
    chrome.runtime = {} as any;
}

Additional context

My current workaround is open up node_modules/darkreader/darkreader.js and surround the entire method body with a try/catch, and log the error to the console. This is not ideal if other errors are thrown.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:17 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
Gustedcommented, Dec 14, 2020
1reaction
Gustedcommented, Jan 25, 2021

As it seems to be solved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React.Exceptions.ReactScriptLoadException (Webpack for ...
Indeed, react-select does not work with server-side rendering, so I updated the webpack sample to demonstrate how it can be imported.
Read more >
Server-side rendering - Apollo GraphQL Docs
Apollo Client provides a handy API for using it with server-side rendering, including a function that executes all of the GraphQL queries that...
Read more >
How to solve "window is not defined" errors in React and Next.js
A different solution is to load your Scroll component using dynamic imports and the srr: false option. This way your component won't even...
Read more >
Headless Chrome: an answer to server-side rendering JS sites
The techniques in this article show how to use the Puppeteer APIs to add server-side rendering (SSR) capabilities to an Express web server....
Read more >
Tag: Server-side Rendering - somewhat abstract
Some of the more common errors relate to browser feature detection being used as ... we just use the import path, which will...
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