Skip to content

Commit 84a0972

Browse files
committed
Play video when page becomes active
1 parent a6336f0 commit 84a0972

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

dotcom-rendering/src/components/LoopVideo.importable.tsx

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export const LoopVideo = ({
148148
const [isAutoplayAllowed, setIsAutoplayAllowed] = useState<boolean | null>(
149149
null,
150150
);
151-
const [isRestoredFromBFCache, setIsRestoredFromBFCache] = useState(false);
151+
const [hasPageBecomeActive, setHasPageBecomeActive] = useState(false);
152152

153153
/**
154154
* Keep a track of whether the video has been in view. We only
@@ -278,6 +278,25 @@ export const LoopVideo = ({
278278
setIsMuted(true);
279279
};
280280

281+
/**
282+
* Handle the case where the page is restored from the cache.
283+
*/
284+
const handleRestoreFromCache = (event: PageTransitionEvent) => {
285+
if (event.persisted) {
286+
setIsAutoplayAllowed(doesUserPermitAutoplay());
287+
setHasPageBecomeActive(true);
288+
} else {
289+
setHasPageBecomeActive(false);
290+
}
291+
};
292+
293+
/**
294+
* Handle the case where the user navigates back to the page.
295+
*/
296+
const handlePageBecomesVisible = () => {
297+
setHasPageBecomeActive(true);
298+
};
299+
281300
document.addEventListener(
282301
customLoopPlayAudioEventName,
283302
handleCustomPlayAudioEvent,
@@ -286,6 +305,14 @@ export const LoopVideo = ({
286305
customYoutubePlayEventName,
287306
handleCustomPlayYoutubeEvent,
288307
);
308+
window.addEventListener('pageshow', function (event) {
309+
handleRestoreFromCache(event);
310+
});
311+
document.addEventListener('visibilitychange', () => {
312+
if (document.visibilityState === 'visible') {
313+
handlePageBecomesVisible();
314+
}
315+
});
289316

290317
return () => {
291318
document.removeEventListener(
@@ -296,6 +323,12 @@ export const LoopVideo = ({
296323
customYoutubePlayEventName,
297324
handleCustomPlayYoutubeEvent,
298325
);
326+
window.removeEventListener('pageshow', function (event) {
327+
handleRestoreFromCache(event);
328+
});
329+
document.removeEventListener('visibilitychange', () => {
330+
handlePageBecomesVisible();
331+
});
299332
};
300333
}, [uniqueId]);
301334

@@ -373,7 +406,7 @@ export const LoopVideo = ({
373406
isInView &&
374407
(playerState === 'NOT_STARTED' ||
375408
playerState === 'PAUSED_BY_INTERSECTION_OBSERVER' ||
376-
(isRestoredFromBFCache && playerState === 'PAUSED_BY_BROWSER'))
409+
(hasPageBecomeActive && playerState === 'PAUSED_BY_BROWSER'))
377410
) {
378411
/**
379412
* Check if the video has not been in view before tracking the play.
@@ -383,7 +416,7 @@ export const LoopVideo = ({
383416
ophanTrackerWeb(atomId, 'loop')('play');
384417
}
385418

386-
setIsRestoredFromBFCache(false);
419+
setHasPageBecomeActive(false);
387420
void playVideo();
388421
}
389422
}, [
@@ -393,7 +426,7 @@ export const LoopVideo = ({
393426
playerState,
394427
playVideo,
395428
hasBeenInView,
396-
isRestoredFromBFCache,
429+
hasPageBecomeActive,
397430
atomId,
398431
]);
399432

@@ -435,30 +468,6 @@ export const LoopVideo = ({
435468
setPreloadPartialData(isAutoplayAllowed === false || !!isInView);
436469
}, [isAutoplayAllowed, isInView]);
437470

438-
/**
439-
* Handle the case where the user navigates back to the page.
440-
*/
441-
useEffect(() => {
442-
const handleRestoreFromCache = (event: PageTransitionEvent) => {
443-
if (event.persisted) {
444-
setIsAutoplayAllowed(doesUserPermitAutoplay());
445-
setIsRestoredFromBFCache(true);
446-
} else {
447-
setIsRestoredFromBFCache(false);
448-
}
449-
};
450-
451-
window.addEventListener('pageshow', function (event) {
452-
handleRestoreFromCache(event);
453-
});
454-
455-
return () => {
456-
window.removeEventListener('pageshow', function (event) {
457-
handleRestoreFromCache(event);
458-
});
459-
};
460-
}, []);
461-
462471
if (renderingTarget !== 'Web') return null;
463472

464473
if (adapted) {

0 commit comments

Comments
 (0)