Skip to content

Commit

Permalink
Fix crash during server render in react 16.4.1. (#13088)
Browse files Browse the repository at this point in the history
* Fix crash during server render.

setTimeout and clearTimeout may not be available in some server-render environments (such as ChakraCore in React.NET), and loading ReactScheduler.js will cause a crash unless the existence of the variables are checked via a typeof comparison.

reactjs/React.NET#555

The crash did not occur in 16.4.0, and the change appears to have been introduced here: https://github.com/facebook/react/pull/12931/files#diff-bbebc3357e1fb99ab13ad796e04b69a6L47

I tested this by using yarn link and running it with a local copy of React.NET. I am unsure the best way to unit test this change, since assigning null to `setTimeout` causes an immediate crash within the Node REPL.

* Fix flow errors and log warning if setTimeout / clearTimeout are
not defined / not a function.

* Use invariant to assert setTimeout / clearTimeout are functions

* Remove use of invariant

* Explain
  • Loading branch information
dustinsoftware authored and gaearon committed Jun 22, 2018
1 parent 076bbea commit c35a1e7
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/react-scheduler/src/ReactScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,15 @@ if (__DEV__) {
// this module is initially evaluated.
// We want to be using a consistent implementation.
const localDate = Date;
const localSetTimeout = setTimeout;
const localClearTimeout = clearTimeout;

// This initialization code may run even on server environments
// if a component just imports ReactDOM (e.g. for findDOMNode).
// Some environments might not have setTimeout or clearTimeout.
// https://github.com/facebook/react/pull/13088
const localSetTimeout =
typeof setTimeout === 'function' ? setTimeout : (undefined: any);
const localClearTimeout =
typeof clearTimeout === 'function' ? clearTimeout : (undefined: any);

const hasNativePerformanceNow =
typeof performance === 'object' && typeof performance.now === 'function';
Expand Down

0 comments on commit c35a1e7

Please sign in to comment.