Home  >  Article  >  Web Front-end  >  How to implement application cache in html5? A brief description of offline storage technology

How to implement application cache in html5? A brief description of offline storage technology

青灯夜游
青灯夜游Original
2018-09-08 17:07:421461browse

In this chapter, we will introduce to you how to implement application cache using html5? Briefly describe offline storage technology. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Before HTML5, we had to connect to the Internet to access the page. However, with the rise of mobile Internet, the location of device terminals is no longer fixed. Mobile devices rely heavily on wireless signals, but the reliability of wireless networks is not stable. For example, websites cannot be accessed when passing through tunnels or in places with weak signal strength. This is undoubtedly a bad user experience, so offline caching in HTML5 (applicationCache) solves this problem.

1: What is offline caching
HTML5 offline cache, also known as applicationCache, is one of the new features of HTML5. A simple understanding is that the data is cached after the first load. Without clearing the cache, it can be loaded next time without a network. It is used to compare web pages or games with static data. Easy to use. Of course, not all new browsers in Html5 can support it, and the same goes for offline caching. Anyway, IE9 (including) and browsers below IE9 are currently not supported. If used on mobile, it should be supported. It is also relatively simple to detect whether offline caching is supported.

2: How to use

HTML5 offline cache is a cache area separated from the browser's cache. To save data in this cache, you need to use a description file (. manifest), lists the resources that need to be downloaded and cached.

1. Import the manifest file







html5 离线缓存

2. After the introduction is completed, the next step is to write the test.mainfest file code.

Manifest files are simple text files that tell the browser what is cached (and what is not cached).

The manifest file can be divided into three parts:
①CACHE MANIFEST - Files listed under this heading will be cached after the first download
②NETWORK - Files listed under this heading require a connection to the server and will not be cached
③FALLBACK - The files listed under this title specify the fallback page when the page cannot be accessed (such as the 404 page)

Complete code

CACHE MANIFEST//必须以这个开头
version 1.0 //最好定义版本,更新的时候只需修改版本号
CACHE:
    m.png
    test.js
    test.css
NETWORK:
    *
FALLBACK
    online.html offline.html

The manifest file needs to be configured with the correct MIME-type, That is "text/cache-manifest", this is configured on the web server.

Analysis:
Lines starting with # represent comments. The files under CACHE are all cached files. NETWORK means that each time it is requested from the network and then cached, the specified file is always requested from the network for the latest one. FALLBACK: If the specified file cannot be found, it will be redirected to a new address. Standards are all capitalized.

3. Dynamically control updates through JS

The application will remain cached after it is offline, unless one of the following situations occurs:
The user clears the browser's access to your website data storage.
The manifest file has been modified.

Please note: Updating a file listed in the manifest does not mean that the browser will re-cache the resource. The manifest file itself must be changed.

1) Cache status: The window.applicationCache object is a programmatic access method to the browser's application cache. Its status property can be used to view the current status of the cache.

The value of applicationCache.status is as follows:
0 === Not cached
​ 1 === Idle (the cache is the latest state)
​ ​ 2 === Checking
​ 3 === Downloading
​ 4 === Update ready
​ 5 === Cache expiration

var appCache = window.applicationCache; 
switch (appCache.status) { 
  case appCache.UNCACHED: // UNCACHED == 0 
    return 'UNCACHED'; 
    break; 
  case appCache.IDLE: // IDLE == 1 
    return 'IDLE'; 
    break; 
  case appCache.CHECKING: // CHECKING == 2 
    return 'CHECKING'; 
    break; 
  case appCache.DOWNLOADING: // DOWNLOADING == 3 
    return 'DOWNLOADING'; 
    break; 
  case appCache.UPDATEREADY: // UPDATEREADY == 4 
    return 'UPDATEREADY'; 
    break; 
  case appCache.OBSOLETE: // OBSOLETE == 5 
    return 'OBSOLETE'; 
    break; 
  default: 
    return 'UKNOWN CACHE STATUS'; 
    break; 
  };

2) Actively update the cache: applicationCache.update()

To update the cache programmatically, please call applicationCache.update() first. This operation will attempt to update the user's cache (provided the manifest file has been changed). Finally, when applicationCache.status is in the UPDATEREADY state, call applicationCache.swapCache() to replace the original cache with the new cache.

var appCache = window.applicationCache;
appCache.update(); // Attempt to update the user's cache.
...
if (appCache.status == window.applicationCache.UPDATEREADY) {
appCache.swapCache();  // The fetch was successful, swap in the new cache.

4. A simple offline cache application

Build a web project AppCache, including four files:
appcache_offline.html



AppCache Test



manifest File: test.manifest

CACHE MANIFEST
#VERSION 1.0
CACHE:
test.css

test.css

output { font: 2em sans-serif; }

test.js

setTimeout(function () {
document.getElementById('test').value = new Date();
}, 1000);



The above is the detailed content of How to implement application cache in html5? A brief description of offline storage technology. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn