How to prevent html5 page from caching?

后端 未结 4 498
遥遥无期
遥遥无期 2020-12-08 04:25

I converted a plain vanilla HTML page to HMTL5/CSS3 with a responsive layout, and for security reasons (dictated by the security people) the page must never cache.

T

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

    I have been struggling with the same issue for quite some time. What works for me - at least so far - in Chrome, FF and IE is doing the following:

    1) reference the manifest file <html lang="nl" manifest="filename.appcache"> From what I understand, this will cache everything that follows in this HTML document, hence a manifest file is needed to prevent this from happening:

    2) use a manifest file filename.appcache with the following content which basically says: for all files, do not read from cache but from network server:

    CACHE MANIFEST
    # 2015-09-25 time 20:33 UTC v 1.01 
    NETWORK:
    *
    


    3) a third step is required: each time you upload a (partial) update of your website, also change the manifest file by changing the date and time stamp in the comment(#) line. Why? Because if you do not change the manifest file, it will not be read and it will default to step 1 and thus cache and read from cache. The fact that the manifest file is changed, however, enforces the manifest file to be read again, and thus enforces that the "do not read from cache but read from network server" instruction therein, is applied again.

    0 讨论(0)
  • 2020-12-08 05:18

    In the beginning of code you need to use this:

    <!DOCTYPE html>
    <html manifest="manifest.appcache">
    ...
    

    Then create manifest.appcache with such content:

    CACHE MANIFEST
    
    # Cache manifest version 1.0
    
    # no cache
    
    NETWORK:
    *
    
    0 讨论(0)
  • 2020-12-08 05:26

    I dislike appcache a tremendous amount. It almost works well but can be a real unforgiving pain. While doing some code refactoring, I realized that after logout, i could browse back to the the last page. Of course, refreshing browser it would force user back to login but this is not desired.

    After searching around and seeing the options, I started to get a bit frustrated. I do not want to use appcache. I then realized that my code was redirecting to login page after destroying the session and got an idea, what if, I redirect to the home page instead? And voila, page was loaded, session checked (and of course not there), user redirected to login. Problem solved.

    0 讨论(0)
  • 2020-12-08 05:30

    The previous answer may not consistently work to prevent caching or to clear existing cache in chrome but there is a work around.

    1) To clear existing cache in chrome, it may be necessary to update all files of the website (eg by linking to a new css file on every page) along with an update of the cache-manifest before existing cache in chrome gets cleared upon the second visit of a page (because of the "flow" of the way in which a page is rendered: the first time a page is visited, the browser reads and caches the manifest , while proceeding with loading from existing cache. Only upon the second visit will the newly stored updated manifest be read and applied).

    2) and if none of that helps, it is possible to include a script in the manifest file itself to check for a new manifest and if found, reload it and use the new manifest. This did the trick and resolved all remaining cases I tested for where files had remained persistently cached in chrome. I found this script on this page by Jason Stimpel.

    <script type="text/javascript">
    window.addEventListener('load', function (e) {
    window.applicationCache.addEventListener('updateready', function (e) {
            window.location.reload();
        }, false);
    }, false);
    </script>
    
    0 讨论(0)
提交回复
热议问题