Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I tell from javascript whether my page was hard refreshed?

I've given up on this, but I thought I'd post here out of curiosity.

What I call a "hard refresh" is the Ctrl+R or Shift+F5 that you do during development to see your changes.

This causes the browser to add a Cache-Control: max-age=0 header to the request and "child" requests like images and scripts, etc.

If you're doing your job, you'll get a 304 on everything but the resource that's changed. (Okay, well, see comments. This is assuming that other validators are sent based on browser caches.)

So far, so good.

The problem is that I'm not loading scripts directly from the page, but through a load.js, and the browsers are inconsistent about whether they include that Cache-Control header on those requests. Chrome doesn't do it at all, and Firefox seems to stop in the middle of a series.

Since I can't access the headers of the current request, there's no way to know whether that header should be included or not.

The result is that when I change a script (other than load.js), a hard refresh does not reliably work, and I have to, e.g., clear the browser cache (which is a bit heavy-handed).

Any thoughts on this?

like image 560
harpo Avatar asked Sep 12 '13 17:09

harpo


People also ask

How do you check if a page has been refreshed in JavaScript?

A better way to know that the page is actually reloaded is to use the navigator object that is supported by most modern browsers. It uses the Navigation Timing API. thanks buddy this is accurate solution to detect either the page is reloaded from right click/refresh from the browser or reloaded by the url.

How do I detect if a browser window is closed or refreshed?

A tab or window closing in a browser can be detected by using the beforeunload event. This can be used to alert the user in case some data is unsaved on the page, or the user has mistakenly navigated away from the current page by closing the tab or the browser.

How do you know if page is refreshed react?

You can check the REDUX or CONTEXT state variables. When the user refreshes the page it reset the CONTEXT or REDUX state and you have to set them manually again. So if they are not set or equal to the initial value which you have given then you can assume that the page is refreshed.

How can I tell if a page is reloaded in jquery?

On Refresh/Reload/F5: If user will refresh the page, first window. onbeforeunload will fire with IsRefresh value = "Close" and then window. onload will fire with IsRefresh value = "Load", so now you can determine at last that your page is refreshing.


1 Answers

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

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

Once load.js detects a hard refresh it can then propagate it to the dependent scripts by e.g. attaching a URL parameter to the requests (think "?t=" + timestamp).

like image 150
Jon Avatar answered Sep 28 '22 09:09

Jon