How to prevent content being displayed from Back-Forward cache in Firefox?

前端 未结 4 1986
既然无缘
既然无缘 2020-12-03 11:42

Browser: Firefox 6.0

I\'ve Page A with the following setup to make sure the content is NOT stored in the bfcache of the browser:

1) $

相关标签:
4条回答
  • 2020-12-03 12:05

    There are multiple caches involved. There's the browser's document cache (bfache), the browser's HTTP cache, and possibly intermediate HTTP caches.

    The <meta> tags you show above have absolutely no effect in current Chrome or Firefox. They may have an effect in IE.

    So chances are, your page is just being read from the browser's HTTP cache.

    If you really want to send no-cache HTTP headers, you should do that. But they need to be actual HTTP headers: as I said above, the <meta> tag "equivalents" do nothing.

    And, importantly, any other intermediate caches are not going to be parsing your HTML so might cache things if you don't actually send the right HTTP headers.

    0 讨论(0)
  • 2020-12-03 12:15

    If you set Cache-Control: "no-cache, no-store, must-revalidate" to http headers the page won't be cached in back-forward cache.

    Firefox also considers event handlers on beforeunload event as a signal to not store page in BFC, but Safari ignores such handlers, so it's better to set correct http headers to indicate the nature of the page content (cacheable or variable)

    0 讨论(0)
  • 2020-12-03 12:19

    There are two caches to bear in mind:

    The bfcache (back-forwards cache)

    The bfcache (in Firefox and Safari) stores the page in memory, including any dynamic modifications to the DOM. It is used by Firefox and Safari when pressing back. To make sure that the page is not stored in this cache, you need to run this line:

    window.addEventListener('unload', function(){}); // Does nothing but break the bfcache in Firefox and Safari
    

    Note that Webkit documentation calls the bfcache the "Page Cache".

    The normal browser cache

    Pages are cached in the normal browser cache, unless you set the proper no-store value in the Cache-Control heading. To be extra sure, send this full header:

    Cache-Control: max-age=0, no-cache, no-store, must-revalidate, private
    

    Firefox and Safari will first check the bfcache when pressing the back button. They will then fall back to the normal cache. So you need to both add an event listener to unload, and set this Cache-Control HTTP header. Note that using <meta> instead of the HTTP header may not work.

    0 讨论(0)
  • 2020-12-03 12:29

    The answer below does not work any more:

    From answer on SO, adding an unload event to window causes the back/forward cache to be cleared.

    UPDATE. POSSIBLE SOLUTION:

    BFCache can bring surprises to developers, because at least in Firefox when moving back/forward the page does not refresh even if it was told by HTTP headers. So it's better to assume that the page will not refresh.

    On the other hand, what is the difference between getting page with outdated data because of BFCache, and finding a tab in your browser that you did not reload for ages?

    If you care about those kind of things, write some javascript that checks server for updates and reloads sensitive information. This is a chance to turn your problem into win ).

    0 讨论(0)
提交回复
热议问题