react-hammerjs icon indicating copy to clipboard operation
react-hammerjs copied to clipboard

Check `document` is not undefined before requiring `hammerjs` for SSR

Open wlingke opened this issue 8 years ago • 3 comments

https://github.com/JedWatson/react-hammerjs/issues/14

wlingke avatar Dec 05 '17 04:12 wlingke

@JedWatson any thoughts on this?

wlingke avatar Dec 12 '17 20:12 wlingke

@bradleyflood You've made a bunch of statements without any backing... I'm not sure why you think "Checking for only window is quite standard." I see no evidence behind that.

On the contrary, I've frequently seen that document is what's checked. window may be mocked for many reasons such as when using webpack's require.ensure. Obviously I have no stats for whether window + document or window or document are more commonly used, but here are at least a number of SSR blog examples that use document alone, without window. It's not the best evidence, but at least shows the community does in fact check for document

https://medium.com/front-end-hacking/preparing-react-components-for-server-side-rendering-18901d09784c https://survivejs.com/webpack/output/server-side-rendering/ http://jxnblk.com/writing/posts/static-site-generation-with-react-and-webpack/ https://github.com/markdalgleish/static-site-generator-webpack-plugin

Also, not sure what this problem is: Another team may even be mocking document = {} on their server side and then they will be in the same scenario you are too.

Suppose you were expecting require('hammerjs') to not be called. This means you previously had typeof(window) === 'undefined'. Regardless of whether you mocked document, this still happens post-PR.

Now suppose you were expecting require('hammerjs') to be called. This means you previously mocked window. If you have also mocked document={}, then the require statement is still called.

So I'm not sure what the problem you're stating is.

The only scenario that has a breaking change is if they were expecting require('hammerjs') to be called, had mocked window and did NOT mock document. I'm not entirely sure this scenario is a frequent use case IMHO...

wlingke avatar Feb 01 '18 08:02 wlingke

@JedWatson I submitted this PR over 3 months ago. Any chance you could spend 10 minutes to review and let me know if this is mergeable? Thanks!

wlingke avatar Mar 13 '18 15:03 wlingke