Safari does not update cache in adjacent tabs when a new service worker is released

Viewed 52

I am using worbox and I ran into a problem. After assembling the files, I execute injectManifest to generate the pre-cached files. Also, I have different strategies for different types of files. I notify users to install a new version via a popup with a confirm button and this works great in chrome. You confirm the installation, through postMessage I send a message and all the tabs are reloaded and the cache is updated. But in safari this does not work, in the same way the tabs are reloaded, but the cache is updated only in 1 tab, in all the others it remains old and so that I do not do the only thing that helps is a closed browser or manually resetting the cache, in all subsequent tabs that I open the cache is updated.

Sample code with installation of a worker and subsequent notification for installation

import {Workbox} from 'https://storage.googleapis.com/workbox-cdn/releases/6.4.2/workbox-window.prod.mjs';

if ('serviceWorker' in navigator) {
    const wb = new Workbox('/pwabuilder-sw.js');
    
    let refreshing;
    navigator.serviceWorker.addEventListener('controllerchange', (event) => {
        if (refreshing) return
        refreshing = true
        window.location.reload()
    });

    const showSkipWaitingPrompt = (event) => {
        document.querySelector('.notification-install-sw').classList.add('show');
        document.getElementById('reload').addEventListener('click', function(e){
            wb.messageSkipWaiting();
            document.querySelector('.offline-with-cache-notification').style.display = 'none';
        });

    };

    wb.addEventListener('waiting', showSkipWaitingPrompt);

    wb.register();
}


if (!navigator.onLine) {
    document.querySelector('.offline-with-cache-notification').classList.add('show');
}

And example code from the worker file itself

importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.4.2/workbox-sw.js');


workbox.googleAnalytics.initialize();


const STYLE_CACHE = "stylesheets";


self.addEventListener('message', (event) => {
    if (event.data && event.data.type === 'SKIP_WAITING') {
        self.skipWaiting();
    }
});

workbox.core.clientsClaim();


// Clean Up Old Precaches
workbox.precaching.cleanupOutdatedCaches();

workbox.precaching.precacheAndRoute(self.__WB_MANIFEST);


workbox.routing.registerRoute(
    ({request, url}) =>
        request.destination === "style",
    new workbox.strategies.StaleWhileRevalidate({
        cacheName: STYLE_CACHE,
        plugins: [
            new workbox.cacheableResponse.CacheableResponsePlugin({
                statuses: [200]
            }),
            new workbox.expiration.ExpirationPlugin({
                maxEntries: 15,
                maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days
                purgeOnQuotaError: true,
            }),
        ],
    })
)
0 Answers
Related