Is swapCache() required in HTML5 offline apps?

Jaka Jančar picture Jaka Jančar · Dec 12, 2009 · Viewed 8.1k times · Source

If I don't implement any updateready event handler and don't call swapCache(), does that mean that the browser will always use the first (oldest) downloaded version of the application?

If no, then why is the swapCache() method needed?

Answer

Johan picture Johan · Sep 12, 2011

Swapcache makes sure that "subsequent" calls to cached resources are taken from the new cache. Subsequent meaning after swapcache. To see this in action try setting the src property of an img dynamically after the swapcache call in the updateready event (so that the browser loads it at that particular time). Make sure this image is not already loaded elsewhere in the page since that will distort this test. Now change the image and change the manifest files (to force reloading the cached files). Reload the page in your browser. You should now see the new version of the image in your rendered page. Now comment out the call to swapcache. Make a change to the manifest file and reload the page and thus all resources. Refresh the page again (to make sure you have a version from the new cache). Now change the image again and change the manifest. Again reload the page: now you see the old version of the image. In the last case, the browser finished loading the new versions in cache, but since swapcache was not called, the image was still taken from the old cache.

If you do not do any dynamic loading of resources in your page, the swapcache has no effect.

In particular, if you reload the page in the updateready event handler calling swapcache first has no effect since reloading the page will get it from the new cache anyway.