Skip to content

Commit

Permalink
Feature detect setTimeout in auto setup for SSR
Browse files Browse the repository at this point in the history
In some server side rendering environments (e.g., `execjs` since
version 2.8.0 [1]) `setTimeout` is not available. Since
`autoSetupTimeout` runs when Video.js is imported and tries to
schedule `autoSetup`, this can lead to errors of the form in server
side rendering:

    TypeError: scheduler is not a function

`autoSetup` already bailed if run outside of a browser environment or
if it has been globally disabled. To prevent calling `setTimeout`, we
move the `Dom.isReal` check into `autoSetupTimeout`. Checking
`options.autoSetup` has to remain in `autoSetup` to preserve backwards
compatiblity with apps that set the option after Video.js has loaded
but before the next tick.

[1] rails/execjs#43
  • Loading branch information
tf committed May 25, 2021
1 parent 2e96945 commit e503d82
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/js/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ let videojs;
*/
const autoSetup = function() {

// Protect against breakage in non-browser environments and check global autoSetup option.
if (!Dom.isReal() || videojs.options.autoSetup === false) {
if (videojs.options.autoSetup === false) {
return;
}

Expand Down Expand Up @@ -71,6 +70,11 @@ const autoSetup = function() {
* The videojs library function
*/
function autoSetupTimeout(wait, vjs) {
// Protect against breakage in non-browser environments
if (!Dom.isReal()) {
return;
}

if (vjs) {
videojs = vjs;
}
Expand Down

0 comments on commit e503d82

Please sign in to comment.