Clearing browserWindow cache upon autoUpdate


#1

When my app updates via autoUpdater, I get everything installed fine. However, when I run my app again, the code for the previous version of my app is stored in a cache and is reused. I wanted to know the best way of clearing the cache upon an update. I’m using the clearCache API and it seems to work fine.

I’d like to prompt the user with a dialog box that asks “An update is available. Would you like to update now or later?” An update does autoUpdater.quitAndInstall(); while later keeps you on the old version of the app until you restart it. Would this be too difficult to do if I wanted to clear the cache only upon a new update? Or is there a simple way to have it done?

The code I’ll be using:

autoupdater.on('update-downloaded' ...
...
    nextWindow.webContents.session.clearCache(function() {
        autoUpdater.quitAndInstall();
    });
...

#2

Okay, this is what I managed to do.

autoUpdater.on('update-downloaded' ...
    updateDownloaded = true;
...
});
...
nextWindow.on('close', function(event) {
    event.preventDefault();
    nextWindow.webContents.session.clearCache(function() {
        console.log("electron cache cleared");
        nextWindow.destroy();
    });
});

The line event.preventDefault(); stops the app from closing immediately when you press X in your app, but allows you to do other things once you do press the X. Once the browserWindow cache has been cleared, you force close the app with browserWindow.destroy();.

So the answer to my own question would be that once I close the browserWindow to update to the newly downloaded version of my app (pressing update), the cache gets cleared. And if I press later, the cache gets cleared only when you close the app again.