{"id":1467,"date":"2019-02-27T15:09:41","date_gmt":"2019-02-27T15:09:41","guid":{"rendered":"https:\/\/guwii.com\/?p=1467"},"modified":"2019-04-26T09:54:20","modified_gmt":"2019-04-26T08:54:20","slug":"cache-issues-with-forwards-and-back-history-in-safari","status":"publish","type":"post","link":"https:\/\/guwii.com\/cache-issues-with-forwards-and-back-history-in-safari\/","title":{"rendered":"Cache issues with Forwards and Back history in Safari"},"content":{"rendered":"\n
Safari has this neat feature in that it saves the page exactly as it was when it was left by the user, so that if you go backwards or forwards to it using the browser buttons (or shortcuts) it doesn\u2019t have to reload the page and instead it can load the saved snapshot state.<\/p>\n\n\n\n
This creates a super smooth feeling when jumping back and forth between sites in your back or forward history, however, it\u2019s not necessarily the experience that users always expect.
One common situation is with hamburger\/drop-down menus, leaving a site whilst the menu is open means that if you go back\/forwards to the site it still remains open, most users would expect the menu to be closed like when you first load a site.<\/p>\n\n\n\n
As a side note it\u2019s worth mentioning that Firefox also uses this caching behaviour in most circumstances, so the solutions we provide also work for Firefox back button history caching issues.<\/p>\n\n\n\n
Now, why are we discussing this, well it was a long rabbit hole of a development issue, on one of our client’s sites we wanted to ensure a few of the features and settings of the site reset when the user used the back button in their Safari browser. This proved particularly difficult to solve. We tested all types of JavaScript event methods, from Eventually, after several hours, we managed to create a reliable solution using a combination of the We began testing a bunch of other sites to see if they had this issue, and surprisingly most sites had this quirk, here\u2019s the Reddit website, for example, the menu stays open when using an iPad’s back\/forward buttons on iOS 9.3:<\/p>\n\n\n\nwindow.onbeforeunload<\/code>,
window.unload<\/code>,
document.visibilitychange<\/code>,
window.onpopstate<\/code> events, through to
.htaccess<\/code> configs to try and create a solution.<\/p>\n\n\n\n
pageshow<\/code>
pagehide<\/code> events.
However, like most things in web development and coding\u2014that wasn\u2019t the end, we had only been testing in iOS 10.3+ and when it came to testing in iOS 9.3 the issue remained, in that the site wasn\u2019t resetting as we hoped. With fingernails firmly dug into our scalps we continued investigating.<\/p>\n\n\n\n