I have been reading the chapter on offline in Dive Into HTML5, and it has left me with some questions.

It says

Every time you make a change to one of the resources in your offline web application, you’ll need to change the cache manifest file itself. This can be as simple as changing a single character. The easiest way I’ve found to accomplish this is to include a comment line with a revision number. Change the revision number in the comment, then the web server will return the newly changed cache manifest file, your browser will notice that the contents of the file have changed, and it will kick off the process to re-download all the resources listed in the manifest.

But let's take the Wikipedia example that is discussed in the same article. Whenever an article is edited, the manifest file must be changed to reflect the edit, and any user that has stored pages offline will lose them because they are not explicitly mentioned in the manifest. Is this really desirable behaviour? If yes, why not do the following:

  1. Store files in the offline cache until they are deleted explicitly, even when the manifest changes
  2. Update files in the cache when they are changed (e.g., when the server does not return a 304 Not modified)

If one were to get the behaviour described in the above two points, what would be his options? Use local storage or something?

有帮助吗?

解决方案

First, check out appcachefacts.info to gain a better understanding of this confusing spec.

AppCache will often work in unexpected ways, like Jake Archibald explains in his blog post Application Cache is a Douchebag.

In it, the behaviour described above is achieved by doing some iframe magic. I got away by just adding a static page to the appcache manifest and loading body content through AJAX calls of non-cached fragment pages, based on external events like onClick, and storing the body data in a WebSQL database for future (possibly offline) reference. This, in effect, had made my offline app entirely JavaScript-based without any page reloads.

In place of WebSQL, you may also use HTML5 local storage but may feel uneasy about the 5MB storage limit.

As to the whys, I can only speculate. I think that:

  1. Appcache will not store files until explicitly deleted, so that the appcache size will remain modest. In the Wikipedia example, it seems a bad idea to cache every visited page and never clear (possibly deleted!) pages.
  2. Files in the manifest are not re-checked on every page reload because this would mean an explosive growth of server requests. If your manifest file contains 30 files, it would send out 30 extra requests on every page load. Even if they are performed asynchronous, this is unacceptable.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top