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

Nuxt 3 PWA doesnt get updated after $pwa.updateServiceWorker() #772

Open
Keinstah opened this issue Oct 22, 2024 · 1 comment
Open

Nuxt 3 PWA doesnt get updated after $pwa.updateServiceWorker() #772

Keinstah opened this issue Oct 22, 2024 · 1 comment

Comments

@Keinstah
Copy link

App not updating the page with latest changes.

I'm calling the $pwa.updateServiceWorker() with window.location = window.location.href to refresh the page when $pwa.needRefresh pops up but it doesn't update the app with the latest changes to the component. I was wondering if i'm setting this up correctly. I'm using the example for Nuxt 3 to get something going.

nuxt.config.ts

{
        scope: '/',
        strategies: 'injectManifest',
        srcDir: './service-worker',
        filename: 'sw.ts',
        manifest: {
            ...
        },
        workbox: {
            globPatterns: ['**/*.{js,css,html,png,svg,ico}'],
        },
        injectManifest: {
            globPatterns: ['**/*.{js,css,html,png,svg,ico}'],
        },
        client: {
            installPrompt: true,
            // you don't need to include this: only for testing purposes
            // if enabling periodic sync for update use 1 hour or so (periodicSyncForUpdates: 3600)
            periodicSyncForUpdates: 20,
        },
        devOptions: {
            enabled: false,
            suppressWarnings: true,
            navigateFallback: '/',
            navigateFallbackAllowlist: [/^\/$/],
            type: 'module',
        },
    }

sw.ts

import {cleanupOutdatedCaches, createHandlerBoundToURL, precacheAndRoute} from 'workbox-precaching';
import {clientsClaim} from 'workbox-core';
import {NavigationRoute, registerRoute} from 'workbox-routing';

declare let self: ServiceWorkerGlobalScope;

const entries = self.__WB_MANIFEST;

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

if (import.meta.env.DEV) {
    entries.push({ url: '/', revision: Math.random().toString() });
}

precacheAndRoute(entries.filter(({ url }) => !['200', '404'].includes(url)));

// clean old assets
cleanupOutdatedCaches();

let allowlist: undefined | RegExp[];
if (import.meta.env.DEV)
    allowlist = [/^\/$/];

// to allow work offline
registerRoute(new NavigationRoute(
    createHandlerBoundToURL('/'),
    {allowlist},
));

self.skipWaiting();
clientsClaim();
@userquin
Copy link
Member

Try creating an empty project via create-pwa, your sw using auto update + prompt logic with registerType using prompt.

https://vite-pwa-org.netlify.app/guide/scaffolding.html

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