prevent ajax call if application cache is being up

2019-08-17 17:02发布

i am writing an offline web application.

i have a manifest files for my applicationCache. and i have handlers for appCache events. i want to detect if the files being downloaded are being downloaded for the first time or being updated. because in case they are being updated, i would like prevent my application code from running, since i will refresh the browser after updating my app.

my specific problem here is that when the "checking" events gets fired, the applicationCache status is already "DOWNLOADING",

does anybody know how to get the applicationCache.status before any manifest or files gets downloaded?

thanks in advance for any answer

window.applicationCache.addEventListener('checking', function (event) {
    console.log("Checking for updates.");
    console.log("inside checking event, appcache status : %s", applicationCache.status);
}, false);

window.applicationCache.addEventListener('updateready', function (e) {
    if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
        // Browser downloaded a new version of manifest files
        window.location.reload();
    }
}, false);

window.applicationCache.addEventListener('downloading', function (event) {
    appCommon.information("New version available", "Updating application files...", null, null);
}, false);

window.applicationCache.addEventListener('progress', function (event) {
    $("#informationModal").find(".modal-body").html("Updating application files... " + event.loaded.toString() + " of " + event.total.toString());
}, false);

window.applicationCache.addEventListener('cached', function (event) {
    $("#informationModal").find(".modal-body").html("Application up to date.");
    setTimeout(function () { $("#informationModal").find(".close").click(); }, 1000);
}, false);

1条回答
别忘想泡老子
2楼-- · 2019-08-17 17:47

According to the specification the checking event is always the first event.

the user agent is checking for an update, or attempting to download the manifest for the first time. This is always the first event in the sequence.

I think it is a mistake to refresh the browser since that will not automatically fetch a new copy. If you look at the MDN guide for using the applicationCache you'll see that files are always loaded from the applicationCache first and then proceed to go through the cycle of events you attempted to avoid by refreshing.

Instead of refreshing you should simply make proper use of the applicationCache event life cycle in order to initialize and start your application.

i would like prevent my application code from running, since i will refresh the browser after updating my app.

You have a lot of control of when your application begins to run, if you really wanted to refresh the browser, just always start the app after updateready or noupdate events, but really instead of refreshing it seems like you should use window.applicationCache.swapCache instead.

I found this question and answer useful.

查看更多
登录 后发表回答