Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
163b8f7
For the first video of the first web story page, use the inlined vide…
coreymasanto Jan 26, 2022
0819baf
Fix merge conflict
coreymasanto Jan 26, 2022
cf79773
Refactor to prevent unnecessary XHR-related function calls
coreymasanto Jan 26, 2022
16ba4e8
Fix promise
coreymasanto Jan 26, 2022
ad8a9a0
Syntax fix
coreymasanto Jan 27, 2022
455d45e
Allow the XHR request to be sent if the inline video is unexpectedly …
coreymasanto Jan 27, 2022
4b3a02f
Lint
coreymasanto Jan 27, 2022
73c67c1
Remove unnecessary variable from shouldUseInlineVideoResponse and upd…
coreymasanto Jan 28, 2022
c92c2e7
Wrap JSON.parse() in a try/catch
coreymasanto Jan 28, 2022
6164988
Pull out a new requestCachedVideoSources() method from the fetchCache…
coreymasanto Jan 28, 2022
7c760e0
Clean up preexisting lengthy if-statement
coreymasanto Jan 28, 2022
b4093ea
Merge branch 'main' into inlinedVideo
coreymasanto Feb 8, 2022
5640b66
Simplify JSON parsing logic in requestCachedVideoSources
coreymasanto Feb 8, 2022
8787143
Add a check for the 'sources' key
coreymasanto Feb 8, 2022
d2465cc
Lint fix
coreymasanto Feb 8, 2022
bc73828
Add initial, untested version of tests for inline video response logic
coreymasanto Feb 11, 2022
697dd6b
Merge branch 'main' into inlinedVideo
coreymasanto Feb 11, 2022
b477b97
Add a describe grouping for the set of inline video response tests
coreymasanto Feb 11, 2022
2d108a6
Update logic for determining whether the inline response should be used
coreymasanto Feb 11, 2022
a5487d2
Get tests passing
coreymasanto Feb 11, 2022
c328d21
Lint fixes
coreymasanto Feb 11, 2022
e0e6de7
Merge branch 'main' into inlinedVideo
coreymasanto Feb 11, 2022
e0e8b85
Merge branch 'main' into inlinedVideo
coreymasanto Feb 11, 2022
d4ed4ec
Merge branch 'main' into inlinedVideo
coreymasanto Feb 14, 2022
db458ea
Merge branch 'main' into inlinedVideo
coreymasanto Feb 14, 2022
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
Prev Previous commit
Next Next commit
Pull out a new requestCachedVideoSources() method from the fetchCache…
…dSources() method
  • Loading branch information
coreymasanto committed Jan 28, 2022
commit 6164988d9441a78c7f53d02522e281bce6e374cf
89 changes: 45 additions & 44 deletions extensions/amp-video/0.1/video-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,50 +47,10 @@ export function fetchCachedSources(

Services.performanceFor(ampdoc.win).addEnabledExperiment('video-cache');

let jsonResponsePromise;
if (shouldUseInlineVideoResponse(videoEl)) {
const inlineResponseEl = win.document.getElementById(
'amp-google-video-cache-response'
);
if (inlineResponseEl?.textContent) {
try {
const inlineResponseJson = JSON.parse(inlineResponseEl.textContent);
jsonResponsePromise = Promise.resolve(inlineResponseJson);
} catch (err) {
// If JSON parsing of the inline response fails, then
// `jsonResponsePromise` will remain undefined and an XHR request will
// be made below.
}
}
}

if (!jsonResponsePromise) {
const {canonicalUrl, sourceUrl} = Services.documentInfoForDoc(win.document);
maybeReplaceSrcWithSourceElement(videoEl, win);
const videoUrl = resolveRelativeUrl(selectVideoSource(videoEl), sourceUrl);
jsonResponsePromise = getCacheUrlService(videoEl, ampdoc)
.then((service) => service.createCacheUrl(videoUrl))
.then((cacheUrl) => {
const requestUrl = addParamsToUrl(
cacheUrl.replace(/\/[ic]\//, '/mbv/'),
{
'amp_video_host_url':
/* document url that contains the video */ canonicalUrl,
'amp_video_require_acao_header': videoEl.hasAttribute('crossorigin')
? 1
: null,
}
);
return Services.xhrFor(win)
.fetch(requestUrl, {prerenderSafe: true})
.then((xhrResponse) => xhrResponse.json());
});
}

return jsonResponsePromise
.then((jsonResponse) => {
applySourcesToVideo(videoEl, jsonResponse['sources'], maxBitrate);
applyAudioInfoToVideo(videoEl, jsonResponse['has_audio']);
return requestCachedVideoSources(videoEl, ampdoc)
.then((response) => {
applySourcesToVideo(videoEl, response['sources'], maxBitrate);
applyAudioInfoToVideo(videoEl, response['has_audio']);
})
.catch(() => {
// If cache fails, video should still load properly.
Expand Down Expand Up @@ -241,6 +201,47 @@ function getCacheUrlService(videoEl, ampdoc) {
.then(() => Services.cacheUrlServicePromiseForDoc(videoEl));
}

/**
* Fetch the sources for the given video element.
* @param {!Element} videoEl
* @param {!AmpDoc} ampdoc
* @return {!Promise<!Object>} JSON representing AMP's cached video sources.
*/
function requestCachedVideoSources(videoEl, ampdoc) {
const {win} = ampdoc;
if (shouldUseInlineVideoResponse(videoEl)) {
const inlineResponseEl = win.document.getElementById(
'amp-google-video-cache-response'
);
if (inlineResponseEl?.textContent) {
Comment thread
coreymasanto marked this conversation as resolved.
Outdated
try {
const inlineResponseJson = JSON.parse(inlineResponseEl.textContent);
return Promise.resolve(inlineResponseJson);
Comment thread
coreymasanto marked this conversation as resolved.
} catch (err) {
// If parsing the response fails, an XHR request will be made below.
}
}
}

const {canonicalUrl, sourceUrl} = Services.documentInfoForDoc(win.document);
maybeReplaceSrcWithSourceElement(videoEl, win);
const videoUrl = resolveRelativeUrl(selectVideoSource(videoEl), sourceUrl);
return getCacheUrlService(videoEl, ampdoc)
.then((service) => service.createCacheUrl(videoUrl))
.then((cacheUrl) => {
const requestUrl = addParamsToUrl(cacheUrl.replace(/\/[ic]\//, '/mbv/'), {
'amp_video_host_url':
/* document url that contains the video */ canonicalUrl,
'amp_video_require_acao_header': videoEl.hasAttribute('crossorigin')
? 1
: null,
});
return Services.xhrFor(win)
.fetch(requestUrl, {prerenderSafe: true})
.then((xhrResponse) => xhrResponse.json());
});
}

/**
* Returns `true` if the video's inline response should be used instead of
* issuing an XHR request.
Expand Down