Can I find out from javascript if my page has been updated?

I refused this, but I thought I would publish it out of curiosity.

What I call a “hard upgrade” is Ctrl + R or Shift + F5 , which you do during development to see your changes.

This forces the browser to add the Cache-Control: max-age=0 header to requests and "child" requests, such as images and scripts, etc.

If you do your job, you will get 304 for everything except the changed resource. (Ok, see the comments. It is assumed that other validators are sent based on browser caches.)

So far so good.

The problem is that I do not load the scripts directly from the page, but through load.js , and the browsers do not agree on whether they include the Cache-Control header in these requests. Chrome doesn't do this at all, and Firefox seems to stop in the middle of the series.

Since I cannot access the headers of the current request, there is no way to know if this header should be included or not.

As a result, I changed the script (except for load.js ), a hard update does not work reliably, and I have to, for example, clear the browser cache (which is a bit heavy).

Any thoughts on this?

+6
source share
2 answers

Unfortunately, you cannot detect a hard update from JavaScript (there is no access to the headers for the page currently loaded).

However, the server can tell from the request headers if this is a hard update, so there is the possibility of cooperation. For example, the server can include a custom <meta> in the response or add a special class to <body> , and your script will have access to this information.

Once load.js detects a hard update, it can then propagate it to dependent scripts, for example. Add a URL parameter to your requests (think "?t=" + timestamp ).

+4
source

You can try checking localStorage. Set the localStorage variable and check it. If he is there, this is not a hard update, otherwise it is a hard update.

-3
source

Source: https://habr.com/ru/post/953721/


All Articles