1

As my experience dictates, when I click BACK in my browser, the previous page shows again, with the starting DOM (at least starting* in the last request sent to its URL) displaying.

(* as returned from the server response; no dom-altering javascript running yet)

Is there a way to make sure the previous page is loaded again (i.e. another actual request to its URL) when the user presses BACK button?

(I expect the answer will be HTTP-related, e.g. headers, and no specific backend dependent - I already know how to send a header with my backend).

Luis Masuelli
  • 12,079
  • 10
  • 49
  • 87
  • If you have to, throw it in an iframe or use ajax to reload the content :P This is probably less than ideal though. – RattleyCooper Mar 17 '16 at 21:32
  • My currently working approach is to use AJAX. It works, but it delays. – Luis Masuelli Mar 17 '16 at 21:32
  • have you tried anything described here? http://stackoverflow.com/questions/158319/cross-browser-onload-event-and-the-back-button Look at Nickolay's answer as well as the accepted one. There is a lot of useful information that may help you. Useability problems are brought up as the user will have slower loading times if the page isn't loading from the cache, so there might be a small delay regardless. – RattleyCooper Mar 17 '16 at 21:36
  • I cannot use jQuery for this. I am still using jQuery right now, but triggering .ready delays with the server load, and the ajax call to restart content also takes time, so I cannot run on any jQuery solution – Luis Masuelli Mar 17 '16 at 21:47
  • Not so quick: quite interesting the onunload event. Will try it – Luis Masuelli Mar 17 '16 at 21:49
  • Also look at the comment I posted on Vitaly's answer. I think that may be why his solution didn't work. Clear your browser cache before hitting back button or you will be loading the cached page without the new meta tags(I think) – RattleyCooper Mar 17 '16 at 21:51
  • Hmmmm no. The unload solution did not produce any effect – Luis Masuelli Mar 17 '16 at 21:53

1 Answers1

3

Add following headers to responses which should not be cached

Cache-Control:no-store
Cache-Control:no-cache
Pragma:no-cache
Expires:Mon, 26 Jul 1997 05:00:00 GMT

Pragma is for HTTP 1.0, just in case you have such users.

Vitaly Kulikov
  • 713
  • 3
  • 13
  • You navigate back from another domain, or from the same site, just another page? And I have one more Cache-Control value in response header, it's no-store. – Vitaly Kulikov Mar 17 '16 at 21:15
  • most of the times will be same site (it implies same fqdn) – Luis Masuelli Mar 17 '16 at 21:26
  • (Note: the date I use is not the same, but still an old one) – Luis Masuelli Mar 17 '16 at 21:27
  • Do you have them only as meta tags or as http headers also? – Vitaly Kulikov Mar 17 '16 at 21:44
  • @LuisMasuelli, did you try clearing your browsers cache before hitting the back button with Vitaly's solution? Because if your browser loads a cached version of the page without these tags, then it wouldn't work like you would expect it to. Follow me? – RattleyCooper Mar 17 '16 at 21:50
  • Yes, but the page is up to date – Luis Masuelli Mar 17 '16 at 21:54
  • @DuckPuncher If you clear the cache, then browser loads page from server. I've tried to use only meta tags, they don't work, probably secret is in headers. – Vitaly Kulikov Mar 17 '16 at 21:56
  • Tried both with meta tags and headers. Neither did work, even considering incognito mode – Luis Masuelli Mar 17 '16 at 22:39
  • @Luis Masuelli I've made end example for you. Here we have page with headers: http://r.kvit.lv/withheaders/ Here is the page w/o headers: http://r.kvit.lv/ You can click and see that header work as expected. And compare with your configuration afterwards. I've checked this in chrome. – Vitaly Kulikov Mar 18 '16 at 09:00
  • Actually I don't know, with those empty pages, how does the content gets cached or not. However, you were close with the solution, but two additional directives were missing in Cache-Control: max-age=0, must-revalidate. Which after a friend of mine putting it, worked as I wanted. – Luis Masuelli Mar 18 '16 at 18:49
  • TBH I understand few to none about browser cache, but this will be my starting point – Luis Masuelli Mar 18 '16 at 18:50
  • IF you add those two headers and give me a brief explanation about why they worked, I accept the answer. Have +1 meanwhile – Luis Masuelli Mar 18 '16 at 18:51
  • **max-age=0** sets resource live time in cache to 0 second. **must-revalidate** says that cache MUST NOT use the entry after it becomes stale to respond to a subsequent request without first revalidating it with the origin server – Vitaly Kulikov Mar 18 '16 at 19:11
  • Note that RFC says that: _Servers SHOULD send the must-revalidate directive if and only if failure to revalidate a request on the entity could result in incorrect operation, such as a silently unexecuted financial transaction._ https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1 – Vitaly Kulikov Mar 18 '16 at 19:28