Skip to content

Commit 820fef0

Browse files
committed
Merge branch 'ph-20250829-1858-supporter-gate' of https://github.com/guardian/dotcom-rendering into ph-20250829-1858-supporter-gate
2 parents f3124ad + ab555c4 commit 820fef0

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

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

Lines changed: 40 additions & 18 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,28 @@ export const LoopVideo = ({
278278
setIsMuted(true);
279279
};
280280

281+
/**
282+
* When the page is restored from BFCache, we need to retrigger autoplay,
283+
* as the video player state will be PAUSED_BY_BROWSER.
284+
*/
285+
const handleRestoreFromCache = (event: PageTransitionEvent) => {
286+
if (event.persisted) {
287+
setIsAutoplayAllowed(doesUserPermitAutoplay());
288+
setHasPageBecomeActive(true);
289+
} else {
290+
setHasPageBecomeActive(false);
291+
}
292+
};
293+
294+
/**
295+
* When a user navigates away from the page and the page is hidden, the video will be
296+
* paused by the browser, as the video is not visible. (e.g. switched tab, minimised window).
297+
* When the page becomes visible again, we need to retrigger autoplay.
298+
*/
299+
const handlePageBecomesVisible = () => {
300+
setHasPageBecomeActive(true);
301+
};
302+
281303
document.addEventListener(
282304
customLoopPlayAudioEventName,
283305
handleCustomPlayAudioEvent,
@@ -286,6 +308,14 @@ export const LoopVideo = ({
286308
customYoutubePlayEventName,
287309
handleCustomPlayYoutubeEvent,
288310
);
311+
window.addEventListener('pageshow', function (event) {
312+
handleRestoreFromCache(event);
313+
});
314+
document.addEventListener('visibilitychange', () => {
315+
if (document.visibilityState === 'visible') {
316+
handlePageBecomesVisible();
317+
}
318+
});
289319

290320
return () => {
291321
document.removeEventListener(
@@ -296,6 +326,12 @@ export const LoopVideo = ({
296326
customYoutubePlayEventName,
297327
handleCustomPlayYoutubeEvent,
298328
);
329+
window.removeEventListener('pageshow', function (event) {
330+
handleRestoreFromCache(event);
331+
});
332+
document.removeEventListener('visibilitychange', () => {
333+
handlePageBecomesVisible();
334+
});
299335
};
300336
}, [uniqueId]);
301337

@@ -373,7 +409,7 @@ export const LoopVideo = ({
373409
isInView &&
374410
(playerState === 'NOT_STARTED' ||
375411
playerState === 'PAUSED_BY_INTERSECTION_OBSERVER' ||
376-
(isRestoredFromBFCache && playerState === 'PAUSED_BY_BROWSER'))
412+
(hasPageBecomeActive && playerState === 'PAUSED_BY_BROWSER'))
377413
) {
378414
/**
379415
* Check if the video has not been in view before tracking the play.
@@ -383,7 +419,7 @@ export const LoopVideo = ({
383419
ophanTrackerWeb(atomId, 'loop')('play');
384420
}
385421

386-
setIsRestoredFromBFCache(false);
422+
setHasPageBecomeActive(false);
387423
void playVideo();
388424
}
389425
}, [
@@ -393,7 +429,7 @@ export const LoopVideo = ({
393429
playerState,
394430
playVideo,
395431
hasBeenInView,
396-
isRestoredFromBFCache,
432+
hasPageBecomeActive,
397433
atomId,
398434
]);
399435

@@ -435,20 +471,6 @@ export const LoopVideo = ({
435471
setPreloadPartialData(isAutoplayAllowed === false || !!isInView);
436472
}, [isAutoplayAllowed, isInView]);
437473

438-
/**
439-
* Handle the case where the user navigates back to the page.
440-
*/
441-
useEffect(() => {
442-
window.addEventListener('pageshow', function (event) {
443-
if (event.persisted) {
444-
setIsAutoplayAllowed(doesUserPermitAutoplay());
445-
setIsRestoredFromBFCache(true);
446-
} else {
447-
setIsRestoredFromBFCache(false);
448-
}
449-
});
450-
}, []);
451-
452474
if (renderingTarget !== 'Web') return null;
453475

454476
if (adapted) {

0 commit comments

Comments
 (0)