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

updateServiceWorker always reload the page #779

Open
wighawag opened this issue Nov 5, 2024 · 5 comments
Open

updateServiceWorker always reload the page #779

wighawag opened this issue Nov 5, 2024 · 5 comments

Comments

@wighawag
Copy link

wighawag commented Nov 5, 2024

Currently in the code

once the service worker is installed, after calling updateServiceWorker the page reload

This is unfortunate as the flow I want for my user is as follow

once a new service worker is detected, I want it installed as soon as possible. Then I ask the user if they want to reload the page to get the new version. If they close, they can continue, but if they reload manually later, I want them to have the new version

updateServiceWorker automatic reload prevent me to do that and so when a user reload they keep getting the message to update until they click "reload". which is not what I want

An option to not reload automatically would be great

@piotr-cz
Copy link
Contributor

@wighawag
Copy link
Author

That demo still do not let me install the service worker unless the click accept which reload the page
I would like the service worker to be installed without reloading the page, and then I ll ask the user if they want to reload to get the newly installed version

See my comment:

once a new service worker is detected, I want it installed as soon as possible. Then I ask the user if they want to reload the page to get the new version. If they close, they can continue, but if they reload manually later, I want them to have the new version

I can achieve that if register.ts did not force the reload.

@piotr-cz
Copy link
Contributor

I'd say that it's doing exactly what you'd it like to do

You can write your own patched registerSW function based on on https://github.com/vite-pwa/vite-plugin-pwa/blob/main/src/client/build/register.ts and use it instead

@wighawag
Copy link
Author

I'd say that it's doing exactly what you'd it like to do

Well, no, as mentioned the demo force you to reload when you accept the changes,

pnpm start
# navigate to localhost => it says offline ready
# ctrl+c 
# tweak the sw (add a console.log line)
pnpm start
# navigate to localhost => it ask to reload
# if you reload or click continue, the message still appears
# if you click reload, it reload and then no message appears

What I want is the following:

pnpm start
# navigate to localhost => it says offline ready
# ctrl+c 
# tweak the sw (add a console.log line)
pnpm start
# navigate to localhost => it ask to reload or continue (but it would let you know the new version is ready)
# if you reload or click continue, the message would NOT appear anymore
# if you click reload, it reload 

You can write your own patched registerSW function based on on https://github.com/vite-pwa/vite-plugin-pwa/blob/main/src/client/build/register.ts and use it instead

do you mean I should patch the npm package ? or is there a way to override the regiser function so that it do not force reload upon accepting the new sw version ?

@piotr-cz
Copy link
Contributor

piotr-cz commented Nov 22, 2024

By patching I mean:

  1. Copy https://github.com/vite-pwa/vite-plugin-pwa/blob/main/src/client/build/register.ts into your source dir
  2. Replace
    - import type { RegisterSWOptions } from '../type'
    + import { RegisterSWOptions } from 'virtual:pwa-register 
  3. Tweak it your way
  4. Use it:
    import { registerSW } from '../<location in source dir>/register.ts'

Or even better:
Prepare pull request where it would be possible to pass a callback either as RegisterSWOptions or additional parameter to updateServiceWorker(), with default value of window.location.reload()

This could potentially fix #498

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