AddType text/cache-manifest .appcache创建 name.manifest,文件后缀可自定义,定义后需要在服务器上添加对应的MIME TYPE
CACHE MANIFEST #VERSION 1.0 CACHE: index.html ./js/jquery.js ./css/style.css NETWORK: ./upload/ FAILBACK: ./proxy/ proxy.html
CACHE MANIFEST:文件标识
#VERSION 1.0 :版本号,只是一行注释,但改变可以更新缓存
CACHE:表示要缓存的文件
NETWORK:表示需要连接服务器的文件
FAILBACK:表示当没有响应时的替代方案
<html manifest="name.appcache">
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. }使用update() 和 swapCache() 不会向用户提供更新的资源,只会让浏览器检查是否有新的cache manifest清单,下载指定的更新内容及重新填充应用缓存。因此需要两次加载才能向用户提供新内容。第一次是获得新的应用缓存,第二次是刷新网页内容。
// Check if a new cache is available on page load. window.addEventListener('load', function(e) { window.applicationCache.addEventListener('updateready', function(e) { if (window.applicationCache.status == window.applicationCache.UPDATEREADY) { // Browser downloaded a new app cache. // Swap it in and reload the page to get the new hotness. window.applicationCache.swapCache(); if (confirm('A new version of this site is available. Load it?')) { window.location.reload(); } } else { // Manifest didn't changed. Nothing new to server. } }, false); }, false);
Document was loaded from Application Cache with manifest http://localhost/fdipzone/test.appcache main.html:31 Application Cache Checking event main.html:31 Application Cache Downloading event main.html:31 Application Cache Progress event (0 of 1) http://localhost/fdipzone/main.html main.html:31 Application Cache Progress event (1 of 1) main.html:31 Application Cache UpdateReady event