开发者

Is swapCache() required in HTML5 offline apps?

If I don't implement any updateready eve开发者_StackOverflow中文版nt 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?


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.


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.


Let's imagine 2 different scenarios.

  1. You call location.reload() when the new content is available. The page will reload using its all-new content. applicationCache.swapCache() is not needed in this case.

  2. Your user continues to interact with your page, without any reload. This interaction causes an asset to load dynamically. For the sake of argument, let's imagine that it's a rollover image, and let's imagine that you have just updated this rollover image. Without applicationCache.swapCache(), your user will continue to see the old rollover image. After applicationCache.swapCache(), s/he will see the new rollover image.

So applicationCache.swapCache() without a reload says: "Keep the page looking the way it was when it was loaded, but use any new assets now, as soon as the page asks for them".


The SwapCache method provides a mechanism for the application to be in control of how an when updates are applied. In regular HTML apps, it can be difficult to determine if the correct JS is present on the clients browser. Also browser implementations vary on when a cache would be updated, I found the iPhone particularly stubborn. swapCache put me back in control of how my app is updated i.e. I could choose to automatically apply the patch or let the user choose when to apply etc.


I was wondering the same thing. I seem to be able to trigger a successful update by just calling "window.applicationCache.update()". If the manifest file has been modified, the 'download' event is triggered, then eventually the "update ready".

When I reload it, it appears to have been applied. I don't seem to need to call swapCache(). I have provision for calling it from the app, but so far have not noticed any effect on the update process.

Calling update() basically eliminates one reload, AFAICS.


swapCache will switch from the previous set of resources listed in the cache manifest (when the running webapp was loaded) to the new set. You are doing this in response to an updateready after all, which signals that a new manifest has been loaded.

This is not to be confused with loading individual resources, for which the usual browser caching policies still apply. In other words you will swap set of resources, but individual resources need their own cache management to ensure they're reloaded when you need them to.

I haven't tried this yet, but it would seem to suggest structuring the code as an "update controller" javascript file that handles the update process, and javascript sources with a versioned filename (or URL) with known entry points.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜