1

I have an web application with a lot of JavaScript. If I update some .js files on the server and I forget to rename them, I have implemented function below in case of an error - the page will be completely reloaded, giving up the cached files:

window.addEventListener('error', function (e) {
  if(confirm("The application encountered an unexpected problem. Click OK to try restarting it")){
        window.location.reload(true);
    }
}

Everything is working well. But not on Chrome. It seems that window.location.reload(true) do not load a fresh version of files from server, because the status code in browser is:

Status Code: 200 OK (from memory cache)

How can I make Chrome based browsers to get a fresh version of all files from server with JavaScript in case of an error? Keep in mind that all .js files are using a long term cache directives and this is the way I want to stay.

Later edit

This is my solution so far (implemented in server side):

<?php
    $path = getcwd() . '/';
    function lastFileStamp($file){
        echo '"'.$path.$file.'?'.filemtime($path.$file).'"';
    }
?>

<link rel="stylesheet" href=<?php lastFileStamp("../common/themes/test.css");?> />
<script src=<?php lastFileStamp("../common/date.js");?> ></script>
...

But this solution is not an answer to the initial question: How can I make Chrome based browsers to get a fresh version of all files from server with JavaScript in case of an error?

Junior
  • 507
  • 5
  • 19
  • Have you checked this answer: https://stackoverflow.com/questions/3715047/how-to-reload-a-page-using-javascript – Ravi Nain Apr 25 '20 at 21:09
  • This is not the case. I do not try to reload the main page, but all .js files linked inside. – Junior Apr 25 '20 at 21:12

1 Answers1

0

You can try to use eTag header to make sure the browser is downloading the latest version of files from the server:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag.

Adam
  • 51
  • 3
  • I do not want a `304 Not Modified` response. I want to take advantage of long term caching without asking server for each file if there is a change in file. I want only in case of an error to load a fresh version of all files from server (just like in Firefox). – Junior Apr 26 '20 at 11:34