B4J Question [BANano] Clear PWA Cache

Toky Olivier

Active Member
Licensed User
Longtime User
Hi,
I made an error in my code. It creates loop. The problem, as it a PWA project, every time I run the project, the old version with loop is started before the corrected one. It blocks my browser and I have to force close. I have to clear the PWA Cache or disable PWA? Can I do it in my Code? I cannot find how to clear it in Google Chrome.

2022-04-23 06_37_43-Gestionnaire des tâches.png
 
Solution
Having a loop error is a nasty one. Testing your app first in debug mode may prevent such a thing. If Chrome still allows you to open the dev tools, you can:

1. unregister the service worker

1650692520170.png


2. clear the application cache

1650692634182.png


A service worker has a build-in system to update its cache if it finds a byte-to-byte difference in the service-worker.js file. Initializing BANano like this with DateTime.Now makes sure it is different with each compilation:

B4X:
BANano.Initialize("BANano", "Activity",DateTime.Now)
BANano.JAVASCRIPT_NAME = "app" & DateTime.Now & ".js"

Tip: Use the extended UseServiceWorker method so you at least 'see' if it updates:
B4X:
#if release...

alwaysbusy

Expert
Licensed User
Longtime User
Having a loop error is a nasty one. Testing your app first in debug mode may prevent such a thing. If Chrome still allows you to open the dev tools, you can:

1. unregister the service worker

1650692520170.png


2. clear the application cache

1650692634182.png


A service worker has a build-in system to update its cache if it finds a byte-to-byte difference in the service-worker.js file. Initializing BANano like this with DateTime.Now makes sure it is different with each compilation:

B4X:
BANano.Initialize("BANano", "Activity",DateTime.Now)
BANano.JAVASCRIPT_NAME = "app" & DateTime.Now & ".js"

Tip: Use the extended UseServiceWorker method so you at least 'see' if it updates:
B4X:
#if release
        BANano.TranspilerOptions.UseServiceWorkerWithUpdateMessage(True, "#26AE60", "Update available", "Click here to update the app to the latest version")
#End If

You could add a switch in your WebApp to force unregister all service workers for this domain and clear the caches:

Snippet:
B4X:
' Usage: https://domain.com/myApp?reset=true
Dim Reset As String = BANano.GetURLParamDefault(BANano.Location.GetHref, "reset", "")
If Reset <> "" Then
      #if javascript
            navigator.serviceWorker.getRegistrations().then( function(registrations) {
                 for(let registration of registrations) {
                      registration.unregister();
                 }
            });
           
            caches.keys().then(function(cacheNames) {
                cacheNames.forEach(function(cacheName) {
                      caches.delete(cacheName);
                });
            });
       #End If
        ' if you want, reload the page without the reset switch
        BANano.Location.Replace(BANano.Location.GetProtocol & "//" & BANano.location.gethost & BANano.location.getpathname)
        Return
end if

Alwaysbusy
 
Upvote 0
Solution

Toky Olivier

Active Member
Licensed User
Longtime User
If Chrome still allows you to open the dev tools
Hello Alain, thank you so much. Chrome didn't allow to open the dev tools but if it comes again, I'll try your Snippet.

Use the extended UseServiceWorker method so you at least 'see' if it updates:
This is activated but default in your template. May be later, we'll need to customize it too as with some framework, the green window is hidden even with that line of code.
Testing your app first in debug mode may prevent such a thing.
I will do this.

Thank you so much.
 
Upvote 0
Top