[Feature Request] Load as an import without exception being thrown when using Server Side Rendering
See original GitHub issueIs 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:
- Created 3 years ago
- Comments:17 (10 by maintainers)
Top GitHub Comments
darkreader.zip #4501
As it seems to be solved.