vite-pwa / docs

Documentation: PWA integrations for Vite and the ecosystem
https://vite-pwa-org.netlify.app/
MIT License
196 stars 42 forks source link

App does not load from SW when refreshing, only when changing route #69

Closed cant89 closed 11 months ago

cant89 commented 11 months ago

We have an app in React, I have added the following configuration to the vite.config.js and when I build the application it properly generates the Service worker in the build folder. If I serve the builded app on localhost I see all the resources to be pre-cached being downloaded and by changing page I see the lazy loaded assets being served from the cache through the ServiceWorker.

However if I reload the window, the assets are still being fetched from the server instead of being retrieved from the cache. How can I fix it?

NOTE:

Here is my config:

plugins: [
  // ... other plugins
  VitePWA({
    registerType: "autoUpdate",
    workbox: {
      globPatterns: ["**/*.{js,css,wasm}"],
    },
    devOptions: {
      enabled: true,
    },
  });
]

Thanks

userquin commented 11 months ago

Check if there are errors when registering the sw: in dev tools, go to Application > Service Worker, the sw should be in green without any error.

Once the sw is activated (should be with a green dot), you can refresh the page and check Network tab, any entry in the sw precache manifest should be served by the service worker

In dev, offiline will not work.

auto update !== auto page reload, you need to use any of the virtual modules: https://vite-pwa-org.netlify.app/guide/auto-update.html#automatic-reload

cant89 commented 11 months ago

Thanks @userquin for your help. The sw seems to be active and running, the only weird thing to me is that deleted at the top, beside localhost:3000

Screenshot 2023-08-04 at 12 49 14

However as said if I refresh the page the assets are not taken from the cache. Only the assets lazily requested subsequently, when changing route, are taken from cache.

Any idea?

userquin commented 11 months ago

Can you try unregistering the sw? In dev tools go to Application > Storage, you should check following checkboxes

Click on Clear site data button, then go to Application > Service Workers and check the current service worker is missing or has the state deleted and restart browser.

cant89 commented 11 months ago

@userquin I did it many times, just never restarted the browser. Is it relevant? Anyway I tried restarting but same behaviour. After clearing the cache:

Screenshot 2023-08-04 at 13 24 28
userquin commented 11 months ago

is the repo public?

cant89 commented 11 months ago

is the repo public?

Unfortunately it's not but please ask and I will share whatever can help

userquin commented 11 months ago

try to provide a minimal repro

cant89 commented 11 months ago

I can't believe it, there was an unregister sw somewhere in the codebase that was unregistering mine as well.

Now it works, sorry for it 🤦