wp/wp-includes/js/heartbeat.js
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
--- a/wp/wp-includes/js/heartbeat.js	Thu Sep 29 08:06:27 2022 +0200
+++ b/wp/wp-includes/js/heartbeat.js	Fri Sep 05 18:40:08 2025 +0200
@@ -223,9 +223,9 @@
 				settings.checkFocusTimer = window.setInterval( checkFocus, 10000 );
 			}
 
-			$(window).on( 'unload.wp-heartbeat', function() {
+			$(window).on( 'pagehide.wp-heartbeat', function() {
 				// Don't connect anymore.
-				settings.suspend = true;
+				suspend();
 
 				// Abort the last request if not completed.
 				if ( settings.xhr && settings.xhr.readyState !== 4 ) {
@@ -233,6 +233,25 @@
 				}
 			});
 
+			$(window).on(
+				'pageshow.wp-heartbeat',
+				/**
+				 * Handles pageshow event, specifically when page navigation is restored from back/forward cache.
+				 *
+				 * @param {jQuery.Event} event
+				 * @param {PageTransitionEvent} event.originalEvent
+				 */
+				function ( event ) {
+					if ( event.originalEvent.persisted ) {
+						/*
+						 * When page navigation is stored via bfcache (Back/Forward Cache), consider this the same as
+						 * if the user had just switched to the tab since the behavior is similar.
+						 */
+						focused();
+					}
+				}
+			);
+
 			// Check for user activity every 30 seconds.
 			window.setInterval( checkUserActivity, 30000 );
 
@@ -541,7 +560,7 @@
 			settings.userActivity = time();
 
 			// Resume if suspended.
-			settings.suspend = false;
+			resume();
 
 			if ( ! settings.hasFocus ) {
 				settings.hasFocus = true;
@@ -550,6 +569,20 @@
 		}
 
 		/**
+		 * Suspends connecting.
+		 */
+		function suspend() {
+			settings.suspend = true;
+		}
+
+		/**
+		 * Resumes connecting.
+		 */
+		function resume() {
+			settings.suspend = false;
+		}
+
+		/**
 		 * Runs when the user becomes active after a period of inactivity.
 		 *
 		 * @since 3.6.0
@@ -593,7 +626,7 @@
 			// Suspend after 10 minutes of inactivity when suspending is enabled.
 			// Always suspend after 60 minutes of inactivity. This will release the post lock, etc.
 			if ( ( settings.suspendEnabled && lastActive > 600000 ) || lastActive > 3600000 ) {
-				settings.suspend = true;
+				suspend();
 			}
 
 			if ( ! settings.userActivityEvents ) {