css-render icon indicating copy to clipboard operation
css-render copied to clipboard

mount.js: Add undefined check before referencing `window`

Open pilotmoon opened this issue 1 year ago • 0 comments

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

This patch adds a small change to the if statement to check the window variable is not undefined before referencing it, following a similar pattern I've seen used in other projects. The reason for the change is that when building a server-side-rendered project using Vite, I get a "ReferenceError: window is not defined" on this line. It builds just fine with the modification in this patch.

Here is the diff that solved my problem:

diff --git a/node_modules/css-render/lib/mount.js b/node_modules/css-render/lib/mount.js
index e1523a0..a78142d 100644
--- a/node_modules/css-render/lib/mount.js
+++ b/node_modules/css-render/lib/mount.js
@@ -5,7 +5,7 @@ exports.mount = exports.unmount = exports.setCount = exports.getCount = void 0;
 const hash_1 = require("./hash");
 const render_1 = require("./render");
 const utils_1 = require("./utils");
-if (window) {
+if (typeof window !== "undefined" && window) {
     window.__cssrContext = {};
 }
 function getCount(el) {

This issue body was partially generated by patch-package.

pilotmoon avatar Nov 16 '23 09:11 pilotmoon