我正在使用应用程序缓存让我的网站也能离线工作。
我的 list 看起来像这样并使用 <iframe>
嵌入:
CACHE:
favicon.ico
assets/css/style.css
assets/js/libs/zepto.js
assets/js/app.js
NETWORK:
*
FALLBACK:
/ offline.html
当我切断互联网连接时它按预期工作 -> 一切都通过 offline.html 页面提供。
但刚才我发现每个 404(例如服务器上从未存在的 URL)也使用 list 中显示 offline.html 的 FALLBACK 部分。
有没有办法区分离线(在这种情况下 offline.html 是正确的选择)和服务器返回 404(在这种情况下我只想显示 404 错误)?
最佳答案
如果原始请求...,Appcache 将回退
...results in a redirect to a resource with another origin (indicative of a captive portal), or a 4xx or 5xx status code or equivalent, or if there were network errors
(来自 http://www.whatwg.org/specs/web-apps/current-work/multipage/offline.html#changesToNetworkingModel)
不幸的是,当回退发生时,找不到原因。您无法区分 500、404、重定向或连接失败。
这是一个 hacky 解决方法:
FALLBACK:
/connection-test.js connection-failed.js
/ offline.html
您可以在 offline.html 页面中嵌入 connection-test.js。 connection-test.js loading 表示用户有连接,connection-failed.js 表示没有连接。
关于html - 应用程序缓存 : don't serve file from appcache on 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13843116/