HTML5 use cache only when offline

2019-03-28 11:12发布

I started to use HTML5 cache to view a simple HTML page with one css file and two js files.

My problem is that the cache is used whether I'm offline or not. But I just want to use the cache when I'm offline. Does anyone know how to solve this?

index.html file manifest:

<html manifest="app.cache">

app.cache manifest file:

CACHE MANIFEST
/index.html
/css/style.css
/js/jquery-1.7.1.min.js
/js/functions.min.js

Thank you!

2条回答
干净又极端
2楼-- · 2019-03-28 11:34

manifest="app.cache" - not going to solve your problem. It caches all the file listed in manifest file. You have to save your data in local storage or in local db and have to retrieve data from server/local based on connection status [online/offline].

查看更多
▲ chillily
3楼-- · 2019-03-28 11:41

According to the standard as given at whatwg, this is possible by changing the cache mode from the default fast to the prefer-online state. There, the instructions given are to add the following at the end of your appcache manifest, after listing all the files you need available offline:

SETTINGS:
prefer-online
NETWORK:
*

Apparently the idea behind this is to allow adding basic offline support to "legacy" applications that cannot help having to change the html document everytime it is served. I have not verified that this works in any current browser.

查看更多
登录 后发表回答