javascript - 更新 HTML5 离线应用程序缓存的正确方法

标签 javascript html html5-appcache

我在网站中使用离线缓存,并使用以下事件处理程序设置来处理更新

applicationCache.addEventListener('updateready', function () {
                    if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
                        window.applicationCache.swapCache();  
                        window.applicationCache.update();
                        window.location.reload();
                    }
                });

它有效,但对我来说似乎多余。我是否需要调用 swapCache() 和 update() 函数,或者简单的 window.location.reload() 就足够了,因为浏览器将在刷新时使用新文件?

最佳答案

实际上根据MSDN文档关于Application Cache API你应该同时使用两者。

来自文档:

In order to swap an old cache out for a new one, call update first. When the status is in the UPDATEREADY state, calling swapCache will make the swap.

Calling swapCache will not update any content on your page. It will simply allow your webpage to be able to access any further dynamic content from the new cache instead of the old one. After the page is refreshed, the newly created cache will be used for all in-page and dynamic requests.

The swapCache method is provided for convenience but is not necessary for basic functionality. Loading or refreshing the page is sufficient. For example, refreshing the page after an UpdateReady event will reload the page from the new cache without a call to swapCache.

swapCache does not cause previously-loaded resources to be reloaded; for example, images do not suddenly get reloaded, and style sheets and scripts do not get reparsed or reevaluated. The only change is that subsequent requests for cached resources will obtain the newer copies.

关于javascript - 更新 HTML5 离线应用程序缓存的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24806441/

相关文章:

javascript - 在 JQuery 中获取祖 parent 的第一个 child 的最优雅、可靠和高效的方法是什么?

javascript - 如何在 windows7(WebRTC,C++)的 chrome(CEF3)中设置音频输出(扬声器)

javascript - 浏览器的 HTTP 缓存曾经用于存储 XMLHttpRequest 响应吗?

javascript - 在基本 Angular Directive(指令)中定义隔离范围时无法获得 '&' 前缀

html - 访问网站 - WWW::Mechanize

javascript - 未捕获的类型错误 : Failed to execute 'createObjectURL' on 'URL' : No function was found that matched the signature provided

javascript - 如何在表头和行中显示嵌套的 JSON 值

json - 我应该使用 Lawnchair 还是 localForage 来离线维护数据

html - 使 HTML5 应用程序可离线使用

javascript - 确保 promise 不会在异步函数结束时泄露