Skip to content

Commit

Permalink
Add lazy playback support
Browse files Browse the repository at this point in the history
  • Loading branch information
Satya Deep Maheshwari committed Feb 25, 2025
1 parent b5216e9 commit 7d25e7a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion plugins/aem-assets-plugin/blocks/video/video.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ main>.section.video-container {
overflow: hidden;
background-color: #53565a;
width: 100%;
height: 2160px;
height: calc(100vh - var(--nav-height));
}

.video-hero .video-hero-content {
Expand Down
28 changes: 15 additions & 13 deletions plugins/aem-assets-plugin/blocks/video/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,21 +562,23 @@ export default async function decorate(block) {
// Check for progressive class
const isProgressive = block.classList.contains('progressive');

// Only load VideoJS if not progressive
if (!isProgressive) {
if (block.classList.contains('delayed')) {
if (typeof window.DELAYED_PHASE !== 'undefined') {
if (window.DELAYED_PHASE) {
loadVideoJs();
} else {
const delayedPhaseHandler = async () => {
document.removeEventListener('delayed-phase', delayedPhaseHandler);
await loadVideoJs();
};
document.addEventListener('delayed-phase', delayedPhaseHandler);
}
let vjsLoadingPhase;
if (block.classList.contains('lazy')) {
vjsLoadingPhase = 'lazy';
} else if (block.classList.contains('delayed')) {
vjsLoadingPhase = 'delayed';
}

if (vjsLoadingPhase) {
if (window.LOADING_PHASE === vjsLoadingPhase) {
loadVideoJs();
} else {
setTimeout(loadVideoJs, 3000);
const vjsLoadingPhaseHandler = async () => {
document.removeEventListener(`${vjsLoadingPhase}-phase`, vjsLoadingPhaseHandler);
await loadVideoJs();
};
document.addEventListener(`${vjsLoadingPhase}-phase`, vjsLoadingPhaseHandler);
}
} else {
await loadVideoJs();
Expand Down
2 changes: 1 addition & 1 deletion scripts/delayed.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
document.dispatchEvent(new Event('delayed-phase'));
Window.DELAYED_PHASE = true;
Window.LOADING_PHASE = 'delayed';
// add delayed functionality here
8 changes: 5 additions & 3 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ async function loadEager(doc) {
decorateMain(main);
document.body.classList.add('appear');
await loadSection(main.querySelector('.section'), waitForFirstImage);
// Artificial Delay eager loading of the page for 2 seconds - for demo only
// Artificial Delay eager loading of the page for 1 seconds - for demo only
await new Promise((resolve) => {
setTimeout(resolve, 2000);
setTimeout(resolve, 1000);
});
}

Expand All @@ -217,6 +217,8 @@ async function loadEager(doc) {
* @param {Element} doc The container element
*/
async function loadLazy(doc) {
document.dispatchEvent(new Event('lazy-phase'));
window.LOADING_PHASE = 'lazy';
const main = doc.querySelector('main');
await loadSections(main);

Expand All @@ -237,7 +239,7 @@ async function loadLazy(doc) {
*/
function loadDelayed() {
// eslint-disable-next-line import/no-cycle
window.setTimeout(() => import('./delayed.js'), 3000);
window.setTimeout(() => import('./delayed.js'), 5000);
// load anything that can be postponed to the latest here
}

Expand Down

0 comments on commit 7d25e7a

Please sign in to comment.