在缓存清单文件 HTML5 的 FALLBACK 部分中使用通配符
如何创建一个支持离线的 Web 应用程序,以便 当用户访问 hxxp://mywebsite/ 并且离线时,会显示 hxxp://mywebsite/offline/ 。 [我的网站中有大约 100 个不同的动态页面,因此我无法将所有页面硬编码到缓存清单文件中]
How to create an offline enabled web-application such that
when user visits hxxp://mywebsite/ and is offline than hxxp://mywebsite/offline/ is displayed. [There are about 100 different dynamic pages in my website, so I cannot afford to hardcode all of them in the cache manifest file]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我引用“manifest.php”而不是“cache.manifest”,然后我的 php 文件如下所示:
文件哈希使其保持最新,以便如果任何文件更改,清单也会更改。希望有帮助:)
I reference "manifest.php" instead of "cache.manifest", then my php file looks like this:
The file hashes keep it up-to-date so that if any files change the manifest changes as well. Hope that helps :)
这将导致整个网站上的所有页面在离线时重定向到离线状态。唯一的问题是声明清单的页面,因为该页面始终被缓存。这意味着您无法在每个页面上声明清单,因为每个访问的页面都会自行缓存而不是重定向。因此,您可以做的是在另一个 html 文件(即 Synchronize.html)上声明您的清单,然后通过存储 cookie 或本地缓存值默认检查您的应用程序是否可以离线使用。如果没有重定向到带有声明的清单的synchronize.html,请设置localcache值,然后重定向回index。
离线很棒SSSSSSSSSS!!!
This will cause all your pages across the entire site to redirect to offline when offline. The only issue is with the page that declares the manifest as that page is always cached. This means you cannot declare the manifest on every page because every visited page will then be cached itself and not redirect. So what you can do is declare your manifest on another html file (IE. Synchronize.html) then from default check whether or not your app has been made available for offline by storing a cookie or localcache value. If not redirect to synchronize.html with the manifest declared, set the localcache value, and redirect back to index.
OFFLINE AWESOMENESSSSSSSSSSS!!!!
不可能在缓存清单中使用通配符,至少据我所知,它在任何当前浏览器中都不起作用。另一种方法可能是动态生成缓存清单,并让脚本生成所有这些后备条目。
It's not possible to use wildcards in the cache manifest, at least it doesn't work in any current browser as far as I'm aware. An alternative approach might be to generate your cache manifest dynamically, and let a script generate all those fallback entries.
在索引页面的不可见 iframe 中引用您的清单文件。这样,您的索引页就不会像通常默认情况那样被缓存,并且您可以完全控制后备...
不需要不可靠的 cookie 或 localStorage!
Reference your manifest file in an invisible iframe in your index page. That way your index page isn't cached as it is normally by default and you have total control over your fallbacks...
No need for unreliable cookies or localStorage!