vite-plugin-pwa icon indicating copy to clipboard operation
vite-plugin-pwa copied to clipboard

auto update should not equal reload page

Open bimohxh opened this issue 2 years ago • 7 comments

I use autoUpdate registerType and the app will reload when new version deployed, but why reload? auto update should only download new version and update local cache, not reload page, reload page is a terrible experience . whether reload page should control by developer. I just want the worker auto download new version, and auto update while user visit the app next time.

bimohxh avatar Dec 22 '22 05:12 bimohxh

@bimohxh it is the way the New sw will control again the app once new version cache ready, check sw lifecycle

userquin avatar Dec 22 '22 09:12 userquin

@userquin I don't think so. just look this code https://github.com/vite-pwa/vite-plugin-pwa/blob/ea1a58bf6219233bddac1b663a8bf49c8e6c888e/src/client/build/register.ts

 if (!event.isUpdate && event.isExternal)
          window.location.reload()
        else if (event.isUpdate)
          auto && window.location.reload()
        else if (!autoDestroy)
          onOfflineReady?.()

  wb?.addEventListener('controlling', (event) => {
            if (event.isUpdate)
              window.location.reload()
          })

bimohxh avatar Dec 23 '22 03:12 bimohxh

@bimohxh I wrote previous code, if you dont reload the window the new sw will not control the page that is being controlled by the current sw version (old one), check sw lifecycle for more info: https://web.dev/service-worker-lifecycle/

userquin avatar Dec 24 '22 19:12 userquin

@userquin thanks. my question is why must controll the page at current session , why not controll at next time user enter the app? the auto mode process now is like:

user enter page --> mybe wait 5 seconds pwa find and download new version ---> auto reload

this is not friendly, my idea process is:

user enter page --> mybe wait 5 seconds pwa find and download new version ---> user enter page next time -> auto update in new version.

I also use pwa plugin @vue/cli-plugin-pwa at vue2, which provides a config like:

import { register } from 'register-service-worker';
register(`${process.env.BASE_URL}service-worker.js`, {
    ready() {},
    ....
   updated() {
      console.log('New content is available; please refresh.');
      // window.location.reload();
    }

as you can seee, I can decide whether need reload page at updated envet.

bimohxh avatar Dec 25 '22 04:12 bimohxh

@bimohxh then use prompt for update strategy, other logic is when there are other clients and the update comes from them.

userquin avatar Dec 25 '22 07:12 userquin

@userquin but the prompt strategy will also disturb user, I just want to update app unconsciously, so I need auto strategy. thanks any way, I will remove window.location.reload from the souce code if no options will be provided.

bimohxh avatar Dec 25 '22 07:12 bimohxh

@bimohxh if you only want the sw to get updated you will need to remove any virtual from your code, Safari users will have problems using the PWA (previous sw will control the app and no the new one): check https://github.com/vite-pwa/vite-plugin-pwa/issues/438#issuecomment-1370171111

userquin avatar Jan 04 '23 11:01 userquin

check the warning here: https://vite-pwa-org.netlify.app/guide/auto-update.html#automatic-reload

userquin avatar Mar 23 '24 02:03 userquin