html - 应用程序缓存 : don't serve file from appcache on 404

标签 html offline html5-appcache

我正在使用应用程序缓存让我的网站也能离线工作。
我的 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/

相关文章:

javascript - 在日期输入中阻止一组日期,这可能吗?

windows - 离线 IIS Inetmgr 扩展服务器节点缓慢(2 分钟)...

javascript - 是否可以使此页面成为 iPhone 的离线网络应用程序?

java - 是否可以使用 Wicket 创建离线应用程序?

ipad - Azure html5 Web应用程序抛出应用程序缓存错误事件: Manifest fetch failed (404)

html - 使用 HTML5 的 AppCache 访问未缓存的文件?

html - 对齐最后一个 div 元素

html - IE 主体宽度不起作用

php - 在 PHP 中下拉

HTML5 仅在不在线时从离线缓存加载页面