Using HTML5, you can easily create an offline version of your web application by creating a cache manifest file.
HTML5 introduces application caching, which means web applications can be cached and accessed when there is no network.
Application caching brings three advantages to apps:
Offline browsing--Users can use them when offline.
Speed--Cached resources load faster.
Reduce server load - the browser will only download changed resources from the server.
As mentioned above, the offline storage of HTML5 is based on a newly created.appcache
file. Through theparsing list
offline storage resources on this file, these resources will be stored like cookies. Later, when the network is offline, the browser will display the page through the data stored offline.
Just like cookies, offline storage of html5 also requires a server environment.
A small tool is provided here - a simple iis server. Place it in the project update directory and double-click to run it to simulate the server environment.
Link: http://pan.baidu.com/s/1jG86UV0 Password: ja9h
Before you start, you need to understand themanifest
(that is, the.appcache
file) and how to write the aboveparsing list
.
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 heading specify the fallback page when the page is inaccessible (such as a 404 page)
When online, the user agent will read the manifest every time it visits the page. If changes are found, all resources in the manifest will be reloaded.
The first line, CACHE MANIFEST, is required:
1 CACHE MANIFEST / theme.css /logo.gif / main.js
The manifest file above lists three resources: a CSS file, a GIF image, and a JavaScript file. When the manifest file loads, the browser downloads these three files from the root directory of the website. Then, whenever the user disconnects from the Internet, these resources are still available.
Whitelist, use the wildcard "*". It will enter the open state of the whitelist. In this state, all URLs that do not appear in the relevant Cache area will use the HTTP related cache header policy by default.
The following NETWORK section specifies that the file "login.asp" will never be cached and is not available offline:
1 NETWORK: login.asp
* can be used to indicate that all other resources/files require an internet connection:
NETWORK: *
The FALLBACK section below specifies that if an Internet connection cannot be established, all files in the /html5/ directory are replaced with "offline.html":
ALLBACK:/html5/ /404.html
Note: The first URI is the resource, the second is the fallback.
Once an app is cached, it remains cached until the following happens:
case/ |-- index.html | |-- demo.appcache | |-- 简易IIS服务器.exe | `-- image |-- HTML5 offline storage principle `-- HTML5 offline storage principle
index.html
HTML5离线存储
demo.appcache
CACHE MANIFEST #v01 image/HTML5 offline storage principle NETWORK:*FALLBACK: /
HTML5 offline storage principle
HTML5 offline storage principle
is stored in the
Okay, then executeSimple IIS Server.exe
Try it out.
When iis is turned on
When iis is turned off (it is turned off, no effect will be seen if paused)
You can see thatPicture 1
has been successfully displayed offline, butPicture 2
cannot be displayed as normal.
Now I want to change the positions ofPicture 2
andPicture 1
.
这时候发现问题来了,html明明修改了为什么图片没有置换过来呢,我不是在demo.appcache
文件的NETWORK
写了星号吗?除了CACHE MANIFEST
文件其它都采用在线模式。查资料得知:引入manifest的页面,即使没有被列入缓存清单中,仍然会被用户代理缓存。
好吧,那我把.appcache
文件更新下,于是乎把头部的版本号修改一下#v02
。刷新下页面还是没反应!再刷新,有了!为什么?
对于浏览器来说,manifest的加载是要晚于其他资源的. 这就导致check manifest的过程是滞后的.发现manifest改变.所有浏览器的实现都是紧随这做静默更新资源.以保证下次pv,应用到更新.
通过控制台我们能够窥探一二:
我们的产品已经更新了用户却要第二次进来才能够看到,这样用户体验也太差了吧,有什么方式能够解决呢?好在html5给javascript提供了相关的API。
API篇幅太多自行查看把,这里我晒下我测试成功的code:
1 /*code1,简单粗暴的*/ 2 applicationCache.onupdateready = function(){ 3 applicationCache.swapCache(); 4 location.reload(); 5 }; 6 /*code2,缓存公用方法*/ 7 // var EventUtil = { 8 // addHandler: function(element, type, handler) { 9 // if (element.addEventListener) { 10 // element.addEventListener(type, handler, false); 11 // } else if (element.attachEvent) { 12 // element.attachEvent(“on” + type, handler); 13 // } else { 14 // element["on" + type] = handler; 15 // } 16 // } 17 // }; 18 // EventUtil.addHandler(applicationCache, “updateready”, function() { //缓存更新并已下载,要在下次进入页面生效 19 // applicationCache.update(); //检查缓存manifest文件是否更新,ps:页面加载默认检查一次。 20 // applicationCache.swapCache(); //交换到新的缓存项中,交换了要下次进入页面才生效 21 // location.reload(); //重新载入页面 22 // });
code1一般用在页面加载时直接触发,而code2的方式可后期检查更新。
文章来源:http://www.codeceo.com/article/html5-cache.html
侵权删