Skip to content
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

Prerender and preload remote video cache. #35272

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build-system/test-configs/forbidden-terms.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ const forbiddenTermsGlobal = {
allowlist: [
'build-system/externs/amp.extern.js',
'extensions/amp-subscriptions-google/0.1/amp-subscriptions-google.js',
'extensions/amp-video/0.1/video-cache.js',
'src/utils/xhr-utils.js',
],
},
Expand Down
16 changes: 12 additions & 4 deletions extensions/amp-video/0.1/amp-video.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ export class AmpVideo extends AMP.BaseElement {
* @nocollapse
*/
static prerenderAllowed(element) {
// Only allow prerender if video sources are cached on CDN, or if video has
// a poster image.
// Only allow prerender if video sources are cached on CDN or remote video
// cache, or if video has a poster image.

// Poster is available.
if (element.getAttribute('poster')) {
// Poster is available, or cache is configured.
if (element.getAttribute('poster') || element.hasAttribute('cache')) {
gmajoulet marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

Expand Down Expand Up @@ -529,6 +529,7 @@ export class AmpVideo extends AMP.BaseElement {
// transcodes generated by the cache.
// Origin sources will only be added when document becomes visible.
sources.forEach((source) => {
// Cached by the AMP Cache (amp-video[amp-orig-src]).
if (isCachedByCdn(source, this.element)) {
source.remove();
const qualities = Object.keys(AMP_VIDEO_QUALITY_BITRATES);
Expand All @@ -548,6 +549,13 @@ export class AmpVideo extends AMP.BaseElement {
}
this.video_.appendChild(currSource);
});

return;
mszylkowski marked this conversation as resolved.
Show resolved Hide resolved
}

// Cached by the remote video caching (amp-video[cache=*]).
if (source.hasAttribute('i-amphtml-video-cached-source')) {
gmajoulet marked this conversation as resolved.
Show resolved Hide resolved
this.video_.appendChild(source);
}
});

Expand Down
91 changes: 90 additions & 1 deletion extensions/amp-video/0.1/test/test-amp-video.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describes.realWin(
const impl = await v.getImpl(false);

if (opt_noLayout) {
return;
return v;
}
if (opt_beforeLayoutCallback) {
opt_beforeLayoutCallback(v, impl);
Expand Down Expand Up @@ -821,6 +821,87 @@ describes.realWin(
expect(sources.length).to.equal(1);
expect(sources[0].getAttribute('src')).to.equal(origSrc);
});

describe('prerendering', () => {
let makeVisible;
let visibilityStubs;

beforeEach(() => {
visibilityStubs = {
getVisibilityState: env.sandbox.stub(
env.ampdoc,
'getVisibilityState'
),
whenFirstVisible: env.sandbox.stub(env.ampdoc, 'whenFirstVisible'),
};
visibilityStubs.getVisibilityState.returns(VisibilityState.PRERENDER);
const visiblePromise = new Promise((resolve) => {
makeVisible = resolve;
});
visibilityStubs.whenFirstVisible.returns(visiblePromise);
});

it('should propagate cached sources in prerendering', async () => {
remoteSources.push({
url: 'https://example.com/cached-video.mp4',
type: 'video/mp4',
});
const source = doc.createElement('source');
source.setAttribute('src', 'https://example.com/video.mp4');
const v = await getVideo(
{
type: 'video/mp4',
cache: 'google',
layout: 'responsive',
height: '100px',
width: '100px',
},
[source],
null,
/* opt_noLayout */ true
);
v.layoutCallback();
gmajoulet marked this conversation as resolved.
Show resolved Hide resolved
const sources = v.querySelectorAll('video source');
// Only one source, and the cached one.
expect(sources.length).to.equal(1);
expect(sources[0].getAttribute('src')).to.equal(
'https://example.com/cached-video.mp4'
);
});

it('should have all sources when visible after prerendering', async () => {
remoteSources.push({
url: 'https://example.com/cached-video.mp4',
type: 'video/mp4',
});
const source = doc.createElement('source');
source.setAttribute('src', 'https://example.com/video.mp4');
const v = await getVideo(
{
type: 'video/mp4',
cache: 'google',
layout: 'responsive',
height: '100px',
width: '100px',
},
[source],
null,
/* opt_noLayout */ true
);
v.layoutCallback();
makeVisible();
await Promise.resolve();
const sources = v.querySelectorAll('video source');
// Cached + non cached source and in the right order.
expect(sources.length).to.equal(2);
expect(sources[0].getAttribute('src')).to.equal(
'https://example.com/cached-video.mp4'
);
expect(sources[1].getAttribute('src')).to.equal(
'https://example.com/video.mp4'
);
});
});
});

describe('blurred image placeholder', () => {
Expand Down Expand Up @@ -1013,6 +1094,14 @@ describes.realWin(
});
});

describe('should prerender remote video cache', () => {
it('with cache="*"', () => {
video.setAttribute('src', 'https://example.com/video.mp4');
video.setAttribute('cache', 'google');
expect(AmpVideo.prerenderAllowed(video)).to.be.true;
});
});

describe('should preconnect to all sources', () => {
let preconnect;

Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-video/0.1/video-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function fetchCachedSources(videoEl, ampdoc) {
'amp_video_host_url':
/* document url that contains the video */ canonicalUrl,
});
return Services.xhrFor(win).fetch(requestUrl);
return Services.xhrFor(win).fetch(requestUrl, {prerenderSafe: true});
})
.then((response) => response.json())
.then((jsonResponse) =>
Expand Down