skip to Main Content

I’m having a html-page which is cachable. At the end of the page, I put:

<script>
    console.log(Date.now());  
</script>

I load the page: The console shows 1695306780514.
I follow a link on that page.
I press the back-button of the browser.

My assumption was that I now get the current datetime, a little bit later than the previous one.

But:

  • Chrome (on Windows) shows the same 1695306780514, not the current datetime in console.
  • Firefox (on Windows) shows just nothing in console.

I even tried

$(document).ready(function() {
    console.log(Date.now());
});   

or

var showDate = function(){
    console.log(Date.now());
};
window.setTimeout(showDate, 1000);

but the effect is exactly the same.

How can I make sure the javascript is executed again and uses the correct datetime?

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to all the hints to https://web.dev/bfcache/. The solution is to use the pageshow-event, which is shown there. Adapted to my problem:

    window.addEventListener('pageshow', (event) => {
      console.log(Date.now());
    });
    

    This works fine.


  2. This is the bf cache in action.

    You can listen to event.persisted and force a reload if necessary:
    https://web.dev/bfcache/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search