This code is in index.html. I call the `updateVersion` with the latest version of my Blazor WebAssembly PWA app. The first line is the original registration of the service worker that was part of the Blazor app template. The rest is added by me.

```
navigator.serviceWorker.register('service-worker.js');

function updateVersion(newVersion) {
    var key = 'x-photish-version';

    var oldVersion = localStorage.getItem(key);

    if (oldVersion == null) {
        localStorage.setItem(key, newVersion);
    }
    else if (newVersion != oldVersion) {
        localStorage.setItem(key, newVersion);

        // Reload service worker
        navigator.serviceWorker.register('service-worker.js').then(function (registration) {

            caches.delete("blazor-resources-/").then(function (e) {
                console.log("'blazor-resources-/' cache deleted");
            });

            registration.update();

            window.location.reload(); 
        }).catch(function (error) {
            // registration failed
            console.log(`Registration failed with ${error}`);
        });
    }
}

```

The version part works. It detects that the version is new and it correctly enters the `newVersion != oldVersion` part of the code where I want to make sure the app is completely refreshed.

To test it, I release a new version of my app with some trivial changes to my app and it detects it's a new version and reloads the page. And my small app changes do not appear. It shows the old version of the app.

It is essential I get a way to do this from the code as I don't want the users to retrieve the latest content on every page load. Only if I actually deployed a new version of the code.

What can I do to ensure the serviceworker is refreshed and the cache of the blazor app itself is not cached?

**UPDATE**: I thought it was solved after changing the code to the following but I am still seeing some cases where it does not work.

```
navigator.serviceWorker.register('service-worker.js');

function updateVersion(newVersion) {
    var key = 'x-photish-version';

    var oldVersion = localStorage.getItem(key);

    if (oldVersion == null) {
        localStorage.setItem(key, newVersion);
    }
    else if (newVersion != oldVersion) {
        localStorage.setItem(key, newVersion);

        caches.delete("blazor-resources-/").then(function (e) {
            console.log("'blazor-resources-/' cache deleted");
        });

        // Reload service worker
        navigator.serviceWorker.register('/service-worker.js', { updateViaCache: 'none' }).then(function (registration) {

            window.location.reload(); 

        }).catch(function (error) {
            // registration failed
            console.log(`Registration failed with ${error}`);
        });
    }
}

```

Still hoping someone out there can fix the code and make it bulletproof or simply declare that this is technically impossible for some reason.