0

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);
Marcelo Biffara
  • 811
  • 4
  • 8

1 Answers1

1

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.

Community
  • 1
  • 1
Bjorn
  • 69,215
  • 39
  • 136
  • 164
  • Thanks for answering, i am already using updateready event, that's where i refresh my browser to guarantee that the user is running the latest code, i can't use swapcache, since that is ok for images, but what's happening here is that the application js has changed, so i need to refresh. My specific problem is that "checking" event is being fired late, when it's fired, the files have already started downloading... what i find useful from your answer though, is initializing my application after update or noupdate events – Marcelo Biffara Oct 07 '14 at 17:54
  • 1
    Your app should be able to load new application JavaScript with swapCache. Try adding a script tag with an src to JavaScript that alerts "foo", and then change that to alerts "bar". It should alert bar without a refresh, just like it should load new images. You might want to reduce your launch JavaScript to just a little bit that you wont have to refresh. – Bjorn Oct 07 '14 at 19:50