@@ -8,6 +8,8 @@ const jsonScript = /** @type {HTMLScriptElement} */ (
88
99const latestSessionTokenStorageKey = 'nocache_bfcache_latest_session_token' ;
1010
11+ const bfcacheInvalidatedStorageKey = 'nocache_bfcache_invalidated' ;
12+
1113/**
1214 * Exports from PHP.
1315 *
@@ -77,12 +79,23 @@ interimLoginBroadcastChannel.addEventListener( 'message', () => {
7779 */
7880function onPageShow ( event ) {
7981 if ( data . debug ) {
80- window . console . info ( 'pageshow, persisted:' , event . persisted ) ; // TODO: Debug.
82+ const adminBar = document . getElementById ( 'wpadminbar' ) ;
8183 if ( event . persisted ) {
82- // @ts -ignore
83- document . getElementById ( 'wpadminbar' ) . style . backgroundColor =
84- 'green' ; // TODO: Debug.
84+ window . console . info (
85+ '[No-cache BFCache] Page restored from bfcache.'
86+ ) ;
87+ if ( adminBar ) {
88+ adminBar . style . backgroundColor = 'green' ; // TODO: Debug.
89+ }
90+ } else if ( sessionStorage . getItem ( bfcacheInvalidatedStorageKey ) ) {
91+ window . console . info (
92+ '[No-cache BFCache] Page invalidated from cache.'
93+ ) ;
94+ if ( adminBar ) {
95+ adminBar . style . backgroundColor = 'red' ; // TODO: Debug.
96+ }
8597 }
98+ sessionStorage . removeItem ( bfcacheInvalidatedStorageKey ) ;
8699 }
87100
88101 const currentSessionTokenString = String ( getCurrentSessionToken ( ) ) ;
@@ -106,17 +119,13 @@ function onPageShow( event ) {
106119 latestSessionToken &&
107120 latestSessionToken !== currentSessionTokenString
108121 ) {
109- // Immediately clear out the contents of the page since otherwise the authenticated content will appear while the page reloads.
110122 if ( data . debug ) {
111- document . body . innerHTML = 'RELOADING DUE TO STALE BFCACHE SESSION' ; // TODO: Temp debug.
112- document . body . style . backgroundColor = 'red' ; // TODO: Temp debug.
113- setTimeout ( ( ) => {
114- window . location . reload ( ) ;
115- } , 2000 ) ; // TODO: Temp debug.
116- } else {
117- document . body . innerHTML = '' ;
118- window . location . reload ( ) ;
123+ sessionStorage . setItem ( bfcacheInvalidatedStorageKey , '1' ) ;
119124 }
125+
126+ // Immediately clear out the contents of the page since otherwise the authenticated content will appear while the page reloads.
127+ document . body . innerHTML = '' ;
128+ window . location . reload ( ) ;
120129 }
121130}
122131
0 commit comments