Top Left Text cha

Website Development Services

The script below, when added to your PWA, will prompt users to install update when available.
<script>
    window.isUpdateAvailable = new Promise(function(resolve, reject) {
        navigator.serviceWorker.register('sw.js')
            .then(reg => {
            reg.onupdatefound = () => {
            const installingWorker = reg.installing;
            installingWorker.onstatechange = () => {
                switch (installingWorker.state) {
                    case 'installed':
                        if (navigator.serviceWorker.controller) {
                            resolve(true);
                        } else {
                            // no update available
                            resolve(false);
                        }
                        break;
                }
            }
        }
    })
        .catch(err => console.error('[SW ERROR]', err));
    });
</script>​

You'll need to replace 'sw.js' with the name of your service worker file.





  • No comments found

Leave your comments

Post comment as a guest

0
Your comments are subjected to administrator's moderation.
X