Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing cache on back-button in Safari 5

As of recent safari 5 was released, and it turns out to cause some problems for my website. I have a dynamic website running classic ASP (though that shouldn't matter much) and the site has some creative use of the history stack. For instance, you can be on a page that lists products, then go to details about a product and change the product (admin-view). When you click save on the product the information is sent to the server via AJAX, and a history.back() is issued. This works great in all browsers (including safari <= 4), however, in the newly released safari 5 it stopped working. It seems that when you click back in safari 5 it doesn't actually refresh the page, it only loads it from cache, which means that the changes made in the details view isn't shown. How can I go about to make this work in safari 5 as well? This is the current code I have to turn off caching (included at the top of every page):

Dim pStr
pStr = "private, no-cache, no-store, must-revalidate"
Response.AddHeader "pragma","no-cache"      '?
Response.AddHeader "cache-control", pStr    '?  Er ikke sikker på om disse 3 siste er nødvendige.
Response.AddHeader "cache-control", "post-check=0, pre-check=0"     '?  Er ikke sikker på om disse 3 siste er nødvendige.
Response.AddHeader "Expires", "Mon, 26 Jul 1997 05:00:00 GMT"       '?
Response.AddHeader "Last-Modified", Now()
like image 854
Alxandr Avatar asked Mar 14 '11 10:03

Alxandr


People also ask

How do you disable cache in Safari?

If you haven't already enable the Develop menu on Safari, with Safari open use the menu to go to Safari > Preferences…. One the preferences window select the Advanced tab. At the bottom there is a checkbox to enable the Develop menu. To disable caching in Safari toggle the menu item under Develop > Disable Caches.

Where does Safari keep cache?

Where to find the Cache files of Safari, Firefox and other Applications? The location of cache files is in your ~/Library/Containers/com. apple. Safari/Data/Library/Caches (earlier versions of macOS: ~/Library/Caches/ ) folder.

What is back forward cache?

November 9, 2022 · 4 min read. Loading a new web page takes time, but about 20% of mobile navigations are actually initiated by the browser Back and Forward buttons. The back/forward cache speeds up these navigation by restoring previously loaded page content.


3 Answers

The empty unload handler will not work anymore. Instead you can check the persisted property of the onpageshow event. It is set to false on initial page load. When page is loaded from bfcache it is set to true.

Kludgish solution is to force a reload when page is loaded from bfcache.

window.onpageshow = function(event) {
    if (event.persisted) {
        window.location.reload() 
    }
};

If you are using jQuery then do:

$(window).bind("pageshow", function(event) {
    if (event.originalEvent.persisted) {
        window.location.reload() 
    }
});
like image 57
Mika Tuupola Avatar answered Oct 24 '22 23:10

Mika Tuupola


After some googeling and digging I've found a solution, though I'm not too happy about it. Setting onunload="" in the body-tag causes Safari to invalidate the page and reload it uppon window.history.back();.

like image 27
Alxandr Avatar answered Oct 25 '22 00:10

Alxandr


Here's another way:

    function invalidateBackCache() {
        // necessary for Safari: mobile & desktop
    }

    window.addEventListener("unload", invalidateBackCache, false);

I chose to go this route because adding HTML (onunload="") to the body tag in .Net involved modifying three files in my case. Setting the onunload attribute in jQuery didn't solve it either.

This works for mobile Sarfari (iPad) as well.

like image 3
webXL Avatar answered Oct 24 '22 22:10

webXL