如何使用 JavaScript 清除应用程序缓存(HTML5 功能)?
我们的 Web 应用程序使用应用程序缓存(缓存清单)在离线模式下恢复 HTML 页面和资源。 HTML 页面将 sessionID 作为 URI 中的参数。因此,在每次注销和登录操作之后,新的 HTML 页面都会保存到应用程序缓存中,因为 URI 中的 sessionId 已更改。使用应用程序几周后,某些浏览器开始运行速度变慢。应用程序缓存的大小(在 FF 3.6+ 上测试)约为 200Mb!每次注销后我们都会清除浏览器的LocalStorage,但是如何清除应用程序存储中的资源呢?
Our Web-Application uses Application cache (cache manifest) to restore HTML page and resources in off-line mode. HTML-pages have sessionIDs as params in URI. So, after each logout and login action new HTML-pages are saved to application cache because sessionId was changed in URI. After some weeks working with application some browsers start work slower. And size of Application cache (tested on FF 3.6+) is about 200Mb! After each logout we clear LocalStorage of browser,but how to clear resources from Application storage?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应用程序缓存占用如此多空间的问题是,您每次都为用户代理提供不同的离线 Web 应用程序。离线 Web 应用程序被识别为用户代理通过缓存清单文件的 URI,包括查询字符串 - 而不是您可能想象的主文件的 URI。
因此,通过在缓存清单 URI 中包含会话 ID,您可以告诉浏览器每个会话都会获得自己的全新应用程序,而不使用任何以前下载的应用程序(因此永远无法清除它们)。您每次都安装不同的网络应用程序。
重新考虑如何构建应用程序,因为当前使用 HTML5 离线缓存清单没有提供任何价值 - 只会导致过度下载。 Web 应用程序鼓励的架构是静态地提供所有 HTML,并通过 AJAX 获取需要会话的数据。当构建在经典的“使用服务器上的数据动态生成 HTML 页面”范例中时,Web 应用程序将无法工作。
The problem with the application cache taking up so much space is that you are giving the user agent a different offline web application each time. An offline web application is identified to the user agent by the URI of the cache manifest file, including query string - not the URI of the master file as you might think.
Thus, by including the session ID in the cache manifest URI, you're telling the browser that each session gets its own brand new application without using any of the previously downloaded ones (and thus, never being able to clear them out). You're installing a different web application every time.
Reconsider how you're architecting your application, as currently using HTML5 offline cache manifest is providing no value - just causing excessive downloading. The architecture that web applications encourage is serving all HTML statically, and fetching data that requires sessions via AJAX. Web applications don't work when built in the classic "dynamically generate an HTML page with data on the server" paradigm.
我不确定您是否可以通过 JavaScript 控制应用程序缓存。这是浏览器和用户在清除缓存时应该处理的事情。
I'm not sure you do have control over application cache from JavaScript. This is something which should be handled by the browser and the user when clearing the cache.