All browsers have their own caching mechanisms, but those mechanisms are unreliable and difficult to control, and may often be bothered by browser cache problems when you do Web development. HTML5 solves some problems through the Applicationcache interface and makes offline storage possible, so that your web app can be accessed offline by the user.
This technique clearly has at least three benefits:
- The immediate benefit is that users can access your web app offline
- Web pages load much faster because the files are cached locally
- offline Applications load only the modified resources, thus greatly reducing the load pressure on the server caused by user requests
How do I implement offline file storage?
Your server has to support html5! first. Yes, it looks like the "My server does not support DIV+CSS" joke that was previously said in the CSS Forest group. But I am serious to tell you that to achieve offline storage applications, you do need server support! Let me take a closer look at HTML5 's offline file storage application What is required for your code, you need to refer to a manifest file in the HTML tag of the page via the manifest property to make your app cacheable. Simply put, the manifest file is a text file that lists the list of files needed for offline access to the application (note: The page that references the manifest file is cached regardless of whether you have listed it), but not only. The code looks like this:
xml/html Code copy content to clipboard
- <HTML manifest="Test.manifest">
- ...
- </html>
Of course, this manifest file path can be used with absolute and relative paths, and can even refer to manifest files on other servers. The file corresponding to the Mime-type should be text/cache-manifest, so you need to configure the server to send the corresponding MIME type information, the server configuration is not within the scope of the discussion, please yourself to understand it. The next thing to look at is the structure of the manifest file, its structure is simple, let's take a look at the following example:
MANIFESTxml/html Code copy content to clipboard
- CACHE MANIFEST
- Index.html
- Style.css
- Images/logo.png
- Scripts/mootools. JS
This is the simplest form of a MANIFEST file, as mentioned earlier, the file lists the list of files that need to be cached, the cache MANIFEST in the first row is required, and each site has 5MB of space to store the data. If the files listed in the manifest file or file cannot be loaded, the entire cache update process will not work and the browser will use the last successful cache. As I said earlier, the manifest file is not just a list of files to cache, so what else does it do? Let's take a look at a slightly more complicated example:
MANIFEST
xml/html Code copy content to clipboard
- CACHE MANIFEST
- # Wanz App V1
- # indicates cache entry
- CACHE:
- Index.html
- Style.css
- Images/logo.png
- Scripts/main.js
- # The following resources must be accessed online
- NETWORK:
- login.php
- # Replace with 404.html if index.php is unreachable
- FALLBACK:
- /index.php/404.html
Experienced students must be able to see that the # is used to annotate a line, but it also has a small effect. The Web app's cache is updated only if the manifest file is modified, so if you just modify the cached file, the user's local cache will not be updated, but you can modify the manifest file to tell the browser that it needs to update the cache. Using this, you can write a file version of this comment as in the example above:
xml/html Code copy content to clipboard
- # Wanz App V1
There are three benefits to this writing:
- You can clearly understand the version of the offline web App
- By simply modifying this version number, you can easily notify the browser to update
- You can complete the cache update with a JavaScript program
As you can see, the manifest file has three nodes, each of which has the following meanings:
CACHE:
This is the default entry for the manifest file, and the files listed after this entry (or files written directly after the cache manifest) are cached when they are downloaded locally
NETWORK:
Optionally, the files listed later in this section require access to the network, even if the user accesses the server offline and skips the cache directly.
FALLBACK:
Optional, used to specify the callback page when the resource is unreachable. Each row consists of two URIs, the first is the resource file Uri, and the second is the callback page URI.
Note: These sections described above are not sequential, and can appear multiple times in the same manifest
So far, you should know how to implement offline file storage, only three steps: 1, configure the server manifest file MIME type, 2, write manifest file; 3, Reference the written manifest file in the manifest property of the HTML tag of the page.
How do I update my offline storage?
You do, even if you unplug the network cable you can also access the page you make. So you start to enrich your page, modify JavaScript code and CSS to achieve a more stunning page effect, and then upload them to the server, you will find that: even if you refresh the page to explode, clear the history of access to the record will not see the new page you want to effect. That's because you haven't updated the HTML5 offline storage file at all. Now the question is how do I update the HTML5 offline cache? Here are three things you can do:
- The user clears the data that is stored offline, which is not necessarily to clean up the browser history, because different browsers manage offline storage in different ways. For example, Firefox offline storage data to "options" + "Advanced" and "Network" and "Offline storage" can be cleared.
- Manifest file is modified, above said, you modify the manifest file listed in the file will not update the cache, but to replace the manifest file
- Writing updates using the JavaScript API
The first two ways is very simple, self-toss it. Let's look at the third, first you need to know about the relevant API, detailed go to view: http://www.whatwg.org/specs/web-apps/current-work/#applicationcache
The following Applicationcache and its several key properties and methods are briefly introduced:
JavaScript Code to copy content to clipboard
- cache = window. Applicationcache
- Returns the Applicationcache object applied to the document of the current Window object
- cache = self. Applicationcache
- Returns the Applicationcache object applied to the current shared worker [shared worker]
- Cache. Status
- Returns the status of the current app's cache state, which has five unsigned short integer values:
- uncached = 0;
- IDLE = 1;
- CHECKING = 2;
- downloading = 3;
- Updateready = 4;
- OBSOLETE = 5;
- Cache. Update ()
- Invoke the current app resource download process
- Cache. SwapCache ()
- Update to the latest cache, this does not cause the previously loaded resources to be suddenly reloaded. The pictures are not reloaded, the styles and scripts are not re-rendered or parsed, and the only change is that the resources that make the request page are up-to-date.
The relationship between the Applicationcache object and the cache host corresponds to one by one, and the Applicationcache property of the Window object returns the Applicationcache object of the active document that is associated with the Window object. When the status property is obtained, it returns the state of the current Applicationcache, which has the following States:
C + + Code copy content to clipboard
- Uncached (value 0)
- At this point, the cache host of the Applicationcache object is not associated with the app cache
- IDLE (value 1)
- The app cache is already up to date and is not marked as obsolete
- CHECKING (value 2)
- Applicationcache
- The cache host for the object is already associated with an app cache, and the update state of the cache is checking
- Downloading (value 3)
- Applicationcache
- The cache host for the object is already associated with an app cache, and the update state of the cache is downloading
- Updateready (value 4)
- Applicationcache
- The cache host for the object is already associated with an app cache, and the update status of the cache is idle and is not marked as obsolete, but the cache is not up-to-date
- OBSOLETE
- (Value 5)
- Applicationcache
- /object's cache host is already associated with an app cache, and the update status of the cache is obsolete
If the Update method is called, the browser user agent must invoke the app cache download process in the background, and if the Swapcache method is called, the browser user agent performs the following steps:
- Checks if the Applicationcache cache host is associated with the app cache
- To make the cache host associated application cache for a Applicationcache object
- If the cache's app cache group is marked as obsolete, then the caches associated with the cached host of the Applicationcache object are canceled and the steps are canceled, and all resources are downloaded from the network instead of from the cache
- Check if there is an app cache with completion flag complete in the same cache group, and the version is newer than cache
- Make the new cache complete marked "complete" the latest app cache
- Cancels the cache host association with the Applicationcache object and replaces the association with the new cache
Assuming that you have written the demo page of the offline file store, you can check the status of the current page cache by following the code:
JavaScript Code to copy content to clipboard
- var appCache = Window.applicationcache;
- Switch (appcache.status) {
- Case appcache.uncached: //uncached = = 0
- Alert ( ' uncached ');
- Break ;
- Case Appcache.idle: //IDLE = 1
- Alert ( ' IDLE ');
- Break ;
- Case appcache.checking: //CHECKING = = 2
- Alert (' CHECKING ');
- Break ;
- Case appcache.downloading: //downloading = = 3
- Alert ( ' downloading ');
- Break ;
- Case Appcache.updateready: //Updateready = = 5
- Alert (' Updateready ');
- Break ;
- Case Appcache.obsolete: //OBSOLETE = = 5
- Alert (' OBSOLETE ');
- Break ;
- Default:
- Alert (' uknown CACHE STATUS ');
- Break ;
- };
The implementation of an update is probably like this: First call Applicationcache.update () allows the browser to start trying to update, if your manifest file is updated (as mentioned earlier, modified version number) When Applicationcache.status is Updateready, you can call Applicationcache.swapcache () to update the old cache to new.
JavaScript Code to copy content to clipboard
- var appCache = Window.applicationcache;
- Appcache.update (); //Start update
- if (Appcache.status = = Window.applicationCache.UPDATEREADY) {
- Appcache.swapcache (); //Get Latest Version cache list and download resources successfully, update cache to latest
- }
Yes, the update process can be very simple, but a good application without fault-tolerant processing, like Ajax technology, you need to monitor the update process, handle a variety of exceptions or prompt waiting state to make your application stronger, the user experience is better, So you need to understand the events triggered by the Applicationcache update process, which are: Onchecking,onerror,onnoupdate,ondownloading,onprogress,onupdateready, Oncached and Onobsolete. Listen to the code of the event you should also be pro, assuming you want to handle the update error, you can write:
JavaScript Code to copy content to clipboard
- var appCache = Window.applicationcache;
- Request to manifest file returns 404 or 410, download failed
- Or the manifest file is modified during the download to trigger the error event
- Appcache.addeventlistener (' error ', Handlecacheerror, false);
- function Handlecacheerror (e) {
- Alert (' Error:cache failed to update! ');
- };
Either the manifest file or the resource file download fails, the entire update process terminates, and the browser uses the latest cache. For more information about the event, go to: http://www.whatwg.org/specs/web-apps/current-work/#event-handlers
After a period of testing, I recommend that you do experiments under Chrome, because the implementation of Firefox is like excrement, there is this function is smelly! , and finally presents an offline app Demo: Meatballs ' Neighbors
Offline storage Getting Started basics