@@ -148,7 +148,7 @@ export const LoopVideo = ({
148
148
const [ isAutoplayAllowed , setIsAutoplayAllowed ] = useState < boolean | null > (
149
149
null ,
150
150
) ;
151
- const [ isRestoredFromBFCache , setIsRestoredFromBFCache ] = useState ( false ) ;
151
+ const [ hasPageBecomeActive , setHasPageBecomeActive ] = useState ( false ) ;
152
152
153
153
/**
154
154
* Keep a track of whether the video has been in view. We only
@@ -278,6 +278,28 @@ export const LoopVideo = ({
278
278
setIsMuted ( true ) ;
279
279
} ;
280
280
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
+
281
303
document . addEventListener (
282
304
customLoopPlayAudioEventName ,
283
305
handleCustomPlayAudioEvent ,
@@ -286,6 +308,14 @@ export const LoopVideo = ({
286
308
customYoutubePlayEventName ,
287
309
handleCustomPlayYoutubeEvent ,
288
310
) ;
311
+ window . addEventListener ( 'pageshow' , function ( event ) {
312
+ handleRestoreFromCache ( event ) ;
313
+ } ) ;
314
+ document . addEventListener ( 'visibilitychange' , ( ) => {
315
+ if ( document . visibilityState === 'visible' ) {
316
+ handlePageBecomesVisible ( ) ;
317
+ }
318
+ } ) ;
289
319
290
320
return ( ) => {
291
321
document . removeEventListener (
@@ -296,6 +326,12 @@ export const LoopVideo = ({
296
326
customYoutubePlayEventName ,
297
327
handleCustomPlayYoutubeEvent ,
298
328
) ;
329
+ window . removeEventListener ( 'pageshow' , function ( event ) {
330
+ handleRestoreFromCache ( event ) ;
331
+ } ) ;
332
+ document . removeEventListener ( 'visibilitychange' , ( ) => {
333
+ handlePageBecomesVisible ( ) ;
334
+ } ) ;
299
335
} ;
300
336
} , [ uniqueId ] ) ;
301
337
@@ -373,7 +409,7 @@ export const LoopVideo = ({
373
409
isInView &&
374
410
( playerState === 'NOT_STARTED' ||
375
411
playerState === 'PAUSED_BY_INTERSECTION_OBSERVER' ||
376
- ( isRestoredFromBFCache && playerState === 'PAUSED_BY_BROWSER' ) )
412
+ ( hasPageBecomeActive && playerState === 'PAUSED_BY_BROWSER' ) )
377
413
) {
378
414
/**
379
415
* Check if the video has not been in view before tracking the play.
@@ -383,7 +419,7 @@ export const LoopVideo = ({
383
419
ophanTrackerWeb ( atomId , 'loop' ) ( 'play' ) ;
384
420
}
385
421
386
- setIsRestoredFromBFCache ( false ) ;
422
+ setHasPageBecomeActive ( false ) ;
387
423
void playVideo ( ) ;
388
424
}
389
425
} , [
@@ -393,7 +429,7 @@ export const LoopVideo = ({
393
429
playerState ,
394
430
playVideo ,
395
431
hasBeenInView ,
396
- isRestoredFromBFCache ,
432
+ hasPageBecomeActive ,
397
433
atomId ,
398
434
] ) ;
399
435
@@ -435,20 +471,6 @@ export const LoopVideo = ({
435
471
setPreloadPartialData ( isAutoplayAllowed === false || ! ! isInView ) ;
436
472
} , [ isAutoplayAllowed , isInView ] ) ;
437
473
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
-
452
474
if ( renderingTarget !== 'Web' ) return null ;
453
475
454
476
if ( adapted ) {
0 commit comments