缓存清单手动更新 [英] Cache manifest manual updates

查看:117
本文介绍了缓存清单手动更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为用户提供选择是否要更新我的Web应用程序的能力。根据我的研究,缓存清单一旦找到修改后的appcache文件,就会自动触发下载事件。目的是在用户首次加载我的应用程序时保持这种行为,但是如果用户已经在缓存中使用了较旧的版本,则他或她将收到确认框:

I want to provide users the ability to choose whether or not they want to update my Web app. From my research, cache manifest automatically fires the "downloading" event as soon as it finds a modified appcache file. The goal is to maintain this behavior if the user loads my app for the first time, but if the user already has an older version on cache, he or she would receive the confirm box:

An update is available. Do you want to download it?
Ok
Cancel

点击确定将触发下载事件,而点击取消则会触发 noupdate事件,从而跳过下载。

Clicking "Ok" would fire the "downloading" event, while clicking "Cancel" would fire the "noupdate" event instead, skipping the download.

我该怎么做?

推荐答案

我认为您正在寻找的确切行为是不可能的。

I don't believe that the exact behavior you are looking for is possible.

由于JavaScript已有效地加载到页面中,因此它将始终在appcache魔术发生后(或同时)执行。

Since JavaScript is effectively loaded into the page, it will always execute after (or while) the appcache magic is happening.

用户首次访问该页面时,浏览器会看到一个清单属性。页面和所有服务器资源将继续正常加载,浏览器将在后台下载并缓存清单文件中指定的所有资源。

The first time a user visits the page, the browser would see that there is a manifest attribute. The page and all server-resources would continue loading normally, and the browser would download and cache all resources specified in the manifest file in the background.

下一次用户访问页面时,它将首先检查清单是否已更新或清单的任何缓存头是否已过期。如果清单是新清单,它将下载更新的缓存并为用户提供版本。在用户刷新页面之前,不会显示新版本。因此,在页面加载并提示用户时,新的应用程序缓存已经下载。

The next time the user visits the page, it first checks to see if the manifest has updated or any caching headers for the manifest have expired. If the manifest is new, it will download the updated cache and present the user with the old version. The new version won't be presented until the user refreshes the page. So, by the time the page loads and you prompt the user, the new appcache has already been downloaded.

尝试此实验。创建带有缓存清单的准简单HTML页面。在标题中包括以下内容:

Try this experiment. Create a bare-bones simple html page with a cache manifest. Include the following in the header:

<script>
var cache = window.applicationCache,
    handleEvent = function(event) {
        console.log("appcache event fired.");
    };

cache.addEventListener('checking', handleEvent);
cache.addEventListener('downloading', handleEvent);
cache.addEventListener('progress', handleEvent);
cache.addEventListener('cached', handleEvent);
cache.addEventListener('noupdate', handleEvent);
cache.addEventListener('updateready', handleEvent);
cache.addEventListener('obsolete', handleEvent);
cache.addEventListener('error', handleEvent);

</script>

观看Chrome开发者控制台。请注意,这里的所有事件处理程序均会在之后触发。Chrome会记录浏览器的内置事件处理程序。

Watch the Chrome developer console. Notice that all the event handlers here fire after Chrome logs the browser's build-in event handlers.

您可以尝试在服务器上检测到缓存清单已更改。如果是这种情况,请直接将用户重定向到包含提示的其他页面,而不是将用户定向到主页。如果用户需要更新的版本,请照常继续。否则,您将需要拦截对应用程序缓存的请求并返回304(未修改)。

What you could try is detect on the server that the cache-manifest has changed. If that is the case, instead of directing the user to the main page, redirect to a different page that will contain the prompt. If the user wants the updated version, continue as normal. If not, you will need to intercept the request for the app-cache and return a 304 (Not Modified).

是很好的资源。

这篇关于缓存清单手动更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆