Force browser to clear cache

后端 未结 17 2389
夕颜
夕颜 2020-11-22 05:04

Is there a way I can put some code on my page so when someone visits a site, it clears the browser cache, so they can view the changes?

Languages used: ASP.NET, VB.N

17条回答
  •  半阙折子戏
    2020-11-22 05:44

    I had similiar problem and this is how I solved it:

    1. In index.html file I've added manifest:

      
      
    2. In section included script updating the cache:

      
      
    3. In section I've inserted onload function:

      
      
    4. In cache.manifest I've put all files I want to cache. It is important now that it works in my case (Apache) just by updating each time the "version" comment. It is also an option to name files with "?ver=001" or something at the end of name but it's not needed. Changing just # version 1.01 triggers cache update event.

      CACHE MANIFEST
      # version 1.01
      style.css
      imgs/logo.png
      #all other files
      

      It's important to include 1., 2. and 3. points only in index.html. Otherwise

      GET http://foo.bar/resource.ext net::ERR_FAILED
      

      occurs because every "child" file tries to cache the page while the page is already cached.

    5. In update_cache.js file I've put this code:

      function checkForUpdate()
      {
          if (window.applicationCache != undefined && window.applicationCache != null)
          {
              window.applicationCache.addEventListener('updateready', updateApplication);
          }
      }
      function updateApplication(event)
      {
          if (window.applicationCache.status != 4) return;
          window.applicationCache.removeEventListener('updateready', updateApplication);
          window.applicationCache.swapCache();
          window.location.reload();
      }
      

    Now you just change files and in manifest you have to update version comment. Now visiting index.html page will update the cache.

    The parts of solution aren't mine but I've found them through internet and put together so that it works.

提交回复
热议问题