gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 23:54:35 24 4
gpt4 key购买 nike

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

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() 就足够了,因为浏览器将在刷新时使用新文件?

最佳答案

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

来自文档:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com