Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make library import safe in non-browser environments #78

Merged
merged 1 commit into from
Sep 6, 2023

Conversation

chetansastry
Copy link
Contributor

window?.MutationObserver raises a ReferenceError when window is not defined (e.g. in Node.js). This code path gets hit just on import. This can be problematic particularly in unit tests and SSR.

Added a typeof guard which should make it not raise error on import.

`window?.MutationObserver` raises a `ReferenceError` when `window` is not defined (e.g. in Node.js). This code path gets hit just on import.

The typeof guard makes it safe for import.
@CLAassistant
Copy link

CLAassistant commented Aug 30, 2023

CLA assistant check
All committers have signed the CLA.

@ajhyndman
Copy link
Member

@chetansastry Thank you for the contribution!

For what it's worth, I am pretty confident that this code path won't be hit until you call init(), rather than on import. That should mean you could also work around this by wrapping your init() call with a try/catch.

With that said, I don't see any reason not to add the extra guard!

@chetansastry
Copy link
Contributor Author

Thanks for accepting!

Here's a testcase to demonstrate the problem (should have included as part of PR, but oh well).

  1. Navigate to https://npm.runkit.com/%40dropbox%2Fttvc (version 2.2.1)
  2. Hit run (just import, no init())

You'll see a ReferenceError: window is not defined since Runkit is node environment without a window object.

@ajhyndman
Copy link
Member

Oh, thank you for sharing the test case!

I see it now. This is because the index file statically invokes getNetworkIdleObservable when exporting the increment/decrement methods.

That shouldn't be the case, so I'll push another fix to address that.

@ajhyndman ajhyndman merged commit 750edb3 into dropbox:main Sep 6, 2023
3 checks passed
@chetansastry chetansastry deleted the chetansastry-patch-1 branch September 7, 2023 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants