HTML5 offline data caching using the application cache interface

Source: Internet
Author: User

1. Configuring the cache file caches manifest

MIME Type:text/cache-manifest
File name: Name.appcache
Role: Used to configure files that need to be cached

2. How to use

To add a MIME TYPE on the server
Add in Apache virtual host

    1. AddType text/cache-manifest. AppCache

Create name.manifest, the file suffix can be customized, after the definition needs to add the corresponding MIME TYPE on the server

    1. CACHE MANIFEST
    2. #VERSION 1.0
    3. CACHE:
    4. Index.html
    5. ./js/jquery.js
    6. ./css/style.css
    7. NETWORK:
    8. ./upload/
    9. Failback:
    10. ./proxy/proxy.html

CACHE MANIFEST: File ID
#VERSION 1.0: Version number, just a line of comments, but changes can update the cache
Cache: Represents the file to be cached
NETWORK: Indicates a file that needs to connect to the server
Failback: Represents an alternative when there is no response


<HTML manifest="Name.appcache">

3. Update method

1. Automatic Update: The browser accesses the app cache on the first visit, and then only updates the cache when the cache manifest file changes (even if the comment changes are updated, #VERSION 1.0), and the cache Changes to the resource file in the manifest do not trigger the update.

2. Manual update: update the cache programmatically, call Applicationcache.update () first, and this action attempts to update the user's cache (provided the cache manifest file has changed). Then, when Applicationcache.status is in the Updateready state, call Applicationcache.swapcache () to replace the original cache with the new cache.

    1. Ar appCache = Window.applicationcache;
    2. Appcache.update ();  //Attempt to update the user ' s cache.
    3. ...
    4. if (Appcache.status = = Window.applicationCache.UPDATEREADY) {
    5. Appcache.swapcache ();  //The fetch is successful, swap in the new cache.
    6. }

Using update () and SwapCache () will not provide the user with updated resources, but will only let the browser check for a new cache manifest manifest, download the specified updates, and repopulate the application cache. Therefore, two loads are required to provide new content to the user. The first time is to get a new app cache, and the second is to refresh the page content.

To avoid the hassle of reloading two times, you can set up listeners to listen for Updateready events when the page loads.

  1. Check If a new cache is available on page load.
  2. Window.addeventlistener (' load ', function (e) {
  3. Window.applicationCache.addEventListener (' updateready ', function (e) {
  4. if (window.applicationCache.status = = Window.applicationCache.UPDATEREADY) {
  5. //Browser downloaded a new app cache.
  6. //Swap it in and reload the page to get the new hotness.
  7. Window.applicationCache.swapCache ();
  8. if (Confirm (' A new version of this site is available. Load it? ')) {  
  9. Window.location.reload ();
  10. }
  11. } Else {
  12. //Manifest didn ' t changed.  Nothing new to server.
  13. }
  14. }, false);
  15. }, false);

4. On-line status detection and monitoring

detection:The Navigator.online property indicates whether the current is online, if true, for online. If False, indicates offline.
monitoring: when the online/offline status switch triggers the Online/offline event, both events are triggered on the BODY element and bubbled up in the order of document.body,document and window.

6. Precautions

1. The capacity limit for site offline storage is 5M
2. If the manifest file, or one of the files listed in the file is not properly downloaded, the entire update process will be considered a failure, the browser continues to use the old cache
3. The HTML that references manifest must be the same as the manifest file, under the same domain
4. Relative path used in manifest, relative reference is manifest file
5. The CACHE manifest string should be on the first line and must be
6. The system automatically caches HTML files that reference the manifest file
7. The cache in the manifest file has no relation to the position order of the network,fallback, and if it is implicitly declared it needs to be in the front
8. Resources in fallback must be the same as manifest files
9. When a resource is cached, the browser directly requests that the absolute path also accesses the resources in the cache.
10. Other pages in the site if the manifest property is not set, the requested resource is also accessed from the cache in the cache
11. When the manifest file changes, the resource request itself triggers the update

HTML5 offline data caching using the application cache interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.