Skip to content

const updateSW = registerSW(...) is not reloading the page #896

@danielalejandromatheus

Description

@danielalejandromatheus

I've started getting into this plugin and it works really great so far, I have not encountered problems until now.
I have a self updating poll that queries every few seconds for testing purposes on a vue component that is mounted at the top level, the function is working like it should, but when I call updateServiceWorker it does not seem to be refreshing the page, maybe I'm missing something in the lifecycle or doing something wrong, this is my code

const { offlineReady, needRefresh, updateServiceWorker } = useRegisterSW({
  onNeedRefresh() {
    useAppConfirmDialog(
      {
        title: 'Update Available',
        message: 'A new version is ready. Refresh to apply it?',
        icon: 'info',
        withCancel: false,
      },
      async () => {
        console.log('Updating service worker...');
        await updateServiceWorker(true);
        console.log('Service worker updated.');
      }
    );
  },
  onRegisteredSW(swUrl, r) {
    if (r) {
      setInterval(async () => {
        if (!r) return;
        if (r.installing || !navigator) return;

        if ('connection' in navigator && !navigator.onLine) return;

        const resp = await fetch(swUrl, {
          cache: 'no-store',
          headers: {
            cache: 'no-store',
            'cache-control': 'no-cache',
          },
        });

        if (resp.status === 200) await r.update();
      }, intervalMS);
    }
  },
});

However it's not working like expected, it's not refreshing the page when I call updateServiceWorker(true), I tried also doing it with the vanilla js events

const updateSW = registerSW({
    onNeedRefresh() {
      console.log('New content available');
      useAppConfirmDialog(
        {
          title: 'Update Available',
          message: 'A new version is ready. Refresh to apply it?',
          icon: 'info',
          withCancel: false,
        },
        async () => {
          await updateSW(true);
        }
      );
    },
    onOfflineReady() {
      console.log('App ready to work offline');
    },
  });

And it's not working either, it only works when I force it with window.location.reload(), but I don't know if it's the expected behavior given the docs.
This is my config, just in case I'm doing something I'm not supposed to

VitePWA({
      registerType: 'prompt',
      injectRegister: 'auto',

      includeAssets: ['favicon.ico', 'robots.txt', 'apple-touch-icon.png'],
      workbox: {
        maximumFileSizeToCacheInBytes: 6 * 1024 * 1024,
        globPatterns: ['**/*.{js,css,html,ico,png,svg,json,webmanifest}'],
        cleanupOutdatedCaches: true,
        clientsClaim: true,
        skipWaiting: true,
        globPatterns: ['**/*.{js,css,html,ico,png,svg,json,webmanifest}'],
        runtimeCaching: [
          {
            urlPattern: ({ request }) => request.mode === 'navigate',
            handler: 'NetworkFirst',
            options: {
              cacheName: 'pages',
              networkTimeoutSeconds: 3,
            },
          },
          {
            urlPattern: ({ request }) =>
              request.destination === 'script' ||
              request.destination === 'style',
            handler: 'StaleWhileRevalidate',
            options: {
              cacheName: 'assets',
            },
          },
        ],
      },
      manifest: {...},
    }),

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions