Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service Worker Not Automatically Detecting New Version in Production Ionic vue vite #775

Open
alexRodriguezCescit opened this issue Oct 23, 2024 · 1 comment

Comments

@alexRodriguezCescit
Copy link

I'm currently trying to detect changes in the Service Worker after pushing a new version to production. The issue I'm facing is that for returning users, when a new version of the website is deployed, they sometimes encounter a blank screen because the old index.js files can no longer be found. To address this, I'm attempting to ensure that the Service Worker detects and updates when a new version is available.

Here's my pwa configuration in vite.config.ts:

VitePWA({
            registerType: "prompt",
            injectRegister: "script",
            workbox: {
                globPatterns: ["**/*.{html,vue}"],
                globIgnores: ["**/*.png", "**/*.svg", "assets/*.svg"],
                cleanupOutdatedCaches: true,
                sourcemap: true,
                skipWaiting: false,
                maximumFileSizeToCacheInBytes: 3000000,
            },
            devOptions: {
                enabled: true,
            },
        }),

Every time i have a service worker in waiting to activate state i have a component to show the prompt in order to reload

ReloadPrompt.vue:

<script setup>
import { useRegisterSW } from "virtual:pwa-register/vue";
console.log(useRegisterSW)
const { needRefresh, updateServiceWorker } = useRegisterSW();
console.log(needRefresh)

async function close() {
    needRefresh.value = false;
}
</script>

<template>
    <div v-if="needRefresh" class="pwa-toast" role="alert">
        <div class="message">
            <span>
                New content available, click on reload button to update.
            </span>
        </div>
        <button @click="updateServiceWorker()">Reload</button>
        <button @click="close">Close</button>
    </div>
</template>

In my current PWA configuration, I'm caching only the index.html and .vue files. I’ve excluded .js files from the cache because the service worker installation was taking too long due to the size of the application (it’s quite large). However, after pushing a new version, the new Service Worker it's not automatically detected,. It seems like the new Service Worker only enters the "waiting to activate" state after realoding the page in about 30 minutes.

Is there any configuration I'm missing that would allow the Service Worker to detect the change immediately when I push a new version to production?

@piotr-cz
Copy link

Try switching to skipWaiting: true, as described here: https://developer.chrome.com/docs/workbox/handling-service-worker-updates

Generated service worker should then contain this:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants