wilsonpage / fastdom

Eliminates layout thrashing by batching DOM measurement and mutation tasks
6.83k stars 240 forks source link

`this` is undefined in global scope #131

Closed hedinne closed 7 months ago

hedinne commented 7 months ago

Hi! 👋

Firstly, thanks for your work on this project! 🙂

I've been using a package called ui-observer for years, which depends on fastdom. I am now using it with the new "App router" in NextJS, which runs fastdom on the server. This should be fine, but for some reason, this is undefined in global scope, but globalThis works.

Can we fix this error? I can submit a pull request if that helps. Here is the diff that solved the problem.

fastdom@1.0.11

diff --git a/node_modules/fastdom/fastdom.js b/node_modules/fastdom/fastdom.js
index 70bf7d7..a0d5833 100644
--- a/node_modules/fastdom/fastdom.js
+++ b/node_modules/fastdom/fastdom.js
@@ -241,4 +241,4 @@ var exports = win.fastdom = (win.fastdom || new FastDom()); // jshint ignore:lin
 if ((typeof define) == 'function') define(function() { return exports; });
 else if ((typeof module) == 'object') module.exports = exports;

-})( typeof window !== 'undefined' ? window : this);
+})( typeof window !== 'undefined' ? window : typeof this !== 'undefined' ? this : globalThis);

Sinceerly, Hedinn

wilsonpage commented 7 months ago

Fixed by #132

hedinne commented 7 months ago

Thank you 🙏