From e1b48042ddee8444a5de0ad339947d3433d530da Mon Sep 17 00:00:00 2001 From: Brandon Casey Date: Tue, 21 Feb 2017 12:03:17 -0500 Subject: [PATCH] fix: synchronously shim vtt.js when possible (#4083) --- src/js/tech/tech.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/js/tech/tech.js b/src/js/tech/tech.js index 667de759f..d315cad55 100644 --- a/src/js/tech/tech.js +++ b/src/js/tech/tech.js @@ -506,7 +506,14 @@ class Tech extends Component { * @fires Tech#vttjserror */ addWebVttScript_() { - if (!window.WebVTT && this.el().parentNode !== null && this.el().parentNode !== undefined) { + if (window.WebVTT) { + return; + } + + // Initially, Tech.el_ is a child of a dummy-div wait until the Component system + // signals that the Tech is ready at which point Tech.el_ is part of the DOM + // before inserting the WebVTT script + if (this.el().parentNode !== null && this.el().parentNode !== undefined) { const vtt = require('videojs-vtt.js'); // load via require if available and vtt.js script location was not passed in @@ -551,7 +558,10 @@ class Tech extends Component { // we don't overwrite the injected window.WebVTT if it loads right away window.WebVTT = true; this.el().parentNode.appendChild(script); + } else { + this.ready(this.addWebVttScript_); } + } /** @@ -567,10 +577,7 @@ class Tech extends Component { remoteTracks.on('addtrack', handleAddTrack); remoteTracks.on('removetrack', handleRemoveTrack); - // Initially, Tech.el_ is a child of a dummy-div wait until the Component system - // signals that the Tech is ready at which point Tech.el_ is part of the DOM - // before inserting the WebVTT script - this.on('ready', this.addWebVttScript_); + this.addWebVttScript_(); const updateDisplay = () => this.trigger('texttrackchange');