Last Updated: February 23, 2017
·
11.56K
· calamari

Disable the Back-Forward-Cache of Firefox and Safari

Just a quick reminder for myself, and because that kind of code lines (or hacks) are weird. I just have to post it.

Reasoning: If you want to write a one page app, that does not reload a page, but offer a machanism to login via URL parameters, you might dislike the Firefox caching the whole app state of the previous page. Because if logging in as User A and then logging in as User B hitting the back button would just show the app in a state that is logged in as User A with all his data. That feels weird. More weird is the solution to this problem:

// Does nothing but break the bfcache of firefox and safari
// On FF, if you use the login via paramater functionality, hitting back after relogin via params
// would serve the whole page from memory, so that you are essentially look at the page and book stuff
// for the previous logged in user. This should clear that memory cache.
+window.addEventListener('unload', function () {});

It seems that adding an window.unload handler to the page disallows Firefox to store the app state in cache. Why this peculiarity works is a mystery to me. If someone has an explanation, I would like to here it.

And there is, where I've got that idea: https://madhatted.com/2013/6/16/you-do-not-understand-browser-history

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside