Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is swapCache() required in HTML5 offline apps?

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?

like image 685
Jaka Jančar Avatar asked Dec 12 '09 19:12

Jaka Jančar


2 Answers

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.

like image 148
Johan Avatar answered Nov 15 '22 22:11

Johan


I have an app with a pretty large cache (>100mb). This takes a particularly long time to swap the cache in (and pretty much locks the browser while this is happening). So I display a message indicating that the app is updating (please wait...), then call swapCache(), then display a new message when it's done indicating completion.

Not sure if this answers your question (as to why it's necessarily needed), but I think it provides a valid use case for swapCache() at least.

like image 24
ggutenberg Avatar answered Nov 15 '22 22:11

ggutenberg