Zizzamia / perfume.js

Web performance library for measuring all performance vitals metrics
https://zizzamia.github.io/perfume/
MIT License
3.12k stars 109 forks source link

Fix import crash under node.js #261

Closed sai-cb closed 6 months ago

sai-cb commented 6 months ago

This library can be imported under node.js for SSR projects (even though it is not actually used).

Because perfume.js is referring to "window" which does not exist under node.js, this is currently causing a crash.

This PR is addressing that issue by using globalThis (which refers to window in browsers, and global in node).

fws407296762 commented 3 months ago

globalThis is only compatible with Chrome 71 or above, lower versions are not compatible。

It is recommended to make it compatible

const getGlobal = function () {
  if (typeof self !== "undefined") {
    return self;
  }
  if (typeof window !== "undefined") {
    return window;
  }
  if (typeof global !== "undefined") {
    return global;
  }
  throw new Error("unable to locate global object");
};
fws407296762 commented 3 months ago

image

sai-cb commented 3 months ago

Fixed in https://github.com/Zizzamia/perfume.js/pull/264