Skip to content

Commit 2b53e04

Browse files
committed
Rerun LoopVideo setup if restored from BFCache
1 parent f023ef2 commit 2b53e04

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ export const LoopVideo = ({
110110
const [currentTime, setCurrentTime] = useState(0);
111111
const [playerState, setPlayerState] =
112112
useState<(typeof PLAYER_STATES)[number]>('NOT_STARTED');
113-
114113
const [isAutoplayAllowed, setIsAutoplayAllowed] = useState<boolean | null>(
115114
null,
116115
);
116+
const [isRestoredFromBFCache, setIsRestoredFromBFCache] = useState(false);
117117

118118
/**
119119
* Keep a track of whether the video has been in view. We only
@@ -259,7 +259,7 @@ export const LoopVideo = ({
259259
handleCustomPlayYoutubeEvent,
260260
);
261261
};
262-
}, [uniqueId]);
262+
}, [uniqueId, isRestoredFromBFCache]);
263263

264264
/**
265265
* Initiates attention tracking for ophan
@@ -404,6 +404,15 @@ export const LoopVideo = ({
404404
setPreloadPartialData(isAutoplayAllowed === false || !!isInView);
405405
}, [isAutoplayAllowed, isInView]);
406406

407+
useEffect(() => {
408+
window.addEventListener('pageshow', function (event) {
409+
if (event.persisted) {
410+
// The page was restored from the bfcache. Rerender the component to ensure video can autoplay.
411+
setIsRestoredFromBFCache(true);
412+
}
413+
});
414+
}, []);
415+
407416
if (renderingTarget !== 'Web') return null;
408417

409418
if (adapted) {

0 commit comments

Comments
 (0)