Prevent ajax call if app cache is updated

I am writing a standalone web application.

I have manifest files for my application. and I have handlers for appCache events. I want to determine if the files are being downloaded for the first time or are being updated. because in case of updating them, I would like to prevent my application code from running, since refreshing the browser after updating my application.

my specific problem is that when the "check" events are fired, the application cache status is already "DOWNLOADING"

Does anyone know how to get applicationCache.status before any manifest or files are loaded?

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);

      

0


source to share


1 answer


According to the specification, an event is checking

always the first event.

the user agent checks for an update or tries 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 as it will not automatically download a new copy. If you look at the MDN manual for usage applicationCache

, you can see that files are always loaded from applicationCache and then go through the loop of events that you tried to avoid by freshening up.

Instead of updating, you should simply use the event lifecycle applicationCache

to initialize and run your application.



I would like my application code not to run as I refresh my browser after updating my application.

You have a lot of control over when your app starts running, if you really want to refresh the browser, always start the app after events updateready

or noupdate

, but in reality, instead of refreshing, you feel like you should use window.applicationCache.swapCache

.

I found this question and answer helpful.

+1


source







All Articles