-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix nasty race condition in AMP video #27740
Changes from 5 commits
c7cb622
87aaf2e
d2a8fcc
ccfcabc
ea12b44
59a5616
9d76adc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,6 @@ import {VisibilityState} from '../../../src/visibility-state'; | |
import { | ||
childElementByTag, | ||
childElementsByTag, | ||
elementByTag, | ||
fullscreenEnter, | ||
fullscreenExit, | ||
insertAfterOrAtStart, | ||
|
@@ -104,15 +103,13 @@ class AmpVideo extends AMP.BaseElement { | |
* @override | ||
*/ | ||
preconnectCallback(opt_onLayout) { | ||
const videoSrc = this.getVideoSourceForPreconnect_(); | ||
if (videoSrc) { | ||
this.getUrlService_().assertHttpsUrl(videoSrc, this.element); | ||
this.getVideoSourcesForPreconnect_().forEach((videoSrc) => { | ||
Services.preconnectFor(this.win).url( | ||
this.getAmpDoc(), | ||
videoSrc, | ||
opt_onLayout | ||
); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
|
@@ -168,21 +165,26 @@ class AmpVideo extends AMP.BaseElement { | |
|
||
/** | ||
* @private | ||
* @return {?string} | ||
* @return {!Array<string>} | ||
*/ | ||
getVideoSourceForPreconnect_() { | ||
if (this.getAmpDoc().getVisibilityState() === VisibilityState.PRERENDER) { | ||
const source = this.getFirstCachedSource_(); | ||
return (source && source.getAttribute('src')) || null; | ||
getVideoSourcesForPreconnect_() { | ||
const videoSrc = this.element.getAttribute('src'); | ||
if (videoSrc) { | ||
return [videoSrc]; | ||
} | ||
let videoSrc = this.element.getAttribute('src'); | ||
if (!videoSrc) { | ||
const source = elementByTag(this.element, 'source'); | ||
if (source) { | ||
videoSrc = source.getAttribute('src'); | ||
const srcs = []; | ||
childElementsByTag(this.element, 'source').forEach((source) => { | ||
const src = source.getAttribute('src'); | ||
if (src) { | ||
srcs.push(src); | ||
} | ||
} | ||
return videoSrc; | ||
// We also want to preconnect to the origin src to make fallback faster. | ||
const origSrc = source.getAttribute('amp-orig-src'); | ||
if (origSrc) { | ||
srcs.push(origSrc); | ||
} | ||
}); | ||
return srcs; | ||
} | ||
|
||
/** @override */ | ||
|
@@ -322,23 +324,38 @@ class AmpVideo extends AMP.BaseElement { | |
// If we are in prerender mode, only propagate cached sources and then | ||
// when document becomes visible propagate origin sources and other children | ||
// If not in prerender mode, propagate everything. | ||
let pendingOriginPromise; | ||
if (this.getAmpDoc().getVisibilityState() == VisibilityState.PRERENDER) { | ||
if (!this.element.hasAttribute('preload')) { | ||
this.video_.setAttribute('preload', 'auto'); | ||
} | ||
this.getAmpDoc() | ||
pendingOriginPromise = this.getAmpDoc() | ||
.whenFirstVisible() | ||
.then(() => { | ||
this.propagateLayoutChildren_(); | ||
// We need to yield to the event queue before listing for loadPromise | ||
// because this element may still be in error state from the pre-render | ||
// load. | ||
return Services.timerFor(this.win) | ||
.promise(1) | ||
.then(() => this.loadPromise(this.video_)); | ||
}); | ||
} else { | ||
this.propagateLayoutChildren_(); | ||
} | ||
|
||
// loadPromise for media elements listens to `loadedmetadata`. | ||
const promise = this.loadPromise(this.video_).then(() => { | ||
this.element.dispatchCustomEvent(VideoEvents.LOAD); | ||
}); | ||
const promise = this.loadPromise(this.video_).then( | ||
() => { | ||
this.element.dispatchCustomEvent(VideoEvents.LOAD); | ||
}, | ||
(reason) => { | ||
if (pendingOriginPromise) { | ||
return pendingOriginPromise; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this one succeeds, we still want to dispatch There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Moved the success handler one further down the chain. |
||
} | ||
throw reason; | ||
} | ||
); | ||
|
||
// Resolve layoutCallback right away if the video won't preload. | ||
if (this.element.getAttribute('preload') === 'none') { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My kingdom for a way to know for sure when the media element ran its media selection process...