stuyam / pressure

:point_down::boom: JavaScript library for handling Force Touch, 3D Touch, and Pointer Pressure.
https://pressurejs.com
MIT License
2.91k stars 98 forks source link

Return early in server environment #25

Closed kylehalleman closed 8 years ago

kylehalleman commented 8 years ago

Currently, the code supplies false if no window or document are found when initiating. However, the code still runs with window and document set to false, causing errors when trying to call methods on false that do not exist (e.g. "Cannot use 'in' operator to search for 'ontouchstart' in false").

This occurs when used in a React app where the initial render is server side, since even if Pressure isn't set until componentDidMount, the import of Pressure triggers initialization, throwing errors. I'd suggest either returning early if there is no window or document, or else make Pressure be explicitly initialized (such as var pressure = Pressure();), at least in an NPM environment.

stuyam commented 8 years ago

Thanks @kylehalleman! To be honest I have done little testing with pressure in npm, I will do some testing with that and try to come up with a fix!

stuyam commented 8 years ago

@kylehalleman just pushed v1.0.1 that should fix this. Let me know if it works for you https://github.com/yamartino/pressure/releases/tag/v1.0.1

kylehalleman commented 8 years ago

Just tested, server rendering works with no errors being thrown, thanks!