Html5缓存,每次用户在线时更新

html5 cache, update every time user is online

本文关键字:在线 更新 用户 缓存 Html5      更新时间:2023-09-26

Html5缓存是一种将网站存储在浏览器内存中的机制,因此您可以在离线时加载它。它读取cache-manifest文件来识别哪些url应该被缓存,哪些不应该。

一旦它缓存你的网站,它将永远卡住,即使你是在线的!你总是会得到网站的缓存版本。除非清单文件的内容被改变。

问题是如何更新html5缓存每次当用户在线?

缓存清单,当你调用:

 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.
 }

但是,manifest文件应该更新,如果文件没有改变,什么也不会发生。因此,您必须生成清单文件。并添加一些注释与lastUpdate时间…

CACHE MANIFEST
# 2010-06-18:v3
# Explicitly cached entries
index.html
css/style.css
# offline.html will be displayed if the user is offline
FALLBACK:
/ /offline.html
# All other resources (e.g. sites) require the user to be online. 
NETWORK:
*
# Additional resources to cache
CACHE:
images/logo1.png
images/logo2.png
images/logo3.png