Skip to content

Commit a6336f0

Browse files
committed
Cleanup event listener
1 parent b27a03a commit a6336f0

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,24 @@ export const LoopVideo = ({
439439
* Handle the case where the user navigates back to the page.
440440
*/
441441
useEffect(() => {
442-
window.addEventListener('pageshow', function (event) {
442+
const handleRestoreFromCache = (event: PageTransitionEvent) => {
443443
if (event.persisted) {
444444
setIsAutoplayAllowed(doesUserPermitAutoplay());
445445
setIsRestoredFromBFCache(true);
446446
} else {
447447
setIsRestoredFromBFCache(false);
448448
}
449+
};
450+
451+
window.addEventListener('pageshow', function (event) {
452+
handleRestoreFromCache(event);
449453
});
454+
455+
return () => {
456+
window.removeEventListener('pageshow', function (event) {
457+
handleRestoreFromCache(event);
458+
});
459+
};
450460
}, []);
451461

452462
if (renderingTarget !== 'Web') return null;

0 commit comments

Comments
 (0)