[WKWebview] Subsequent SW Updates Not Setting `needRefresh`
daniel-nagy opened this issue · comments
I'm not sure if the issue is in this plugin, Workbox, or WKWebview. The first time I deploy an update to my service worker needRefresh
is true
and I'm able to show an update notification and reload the page and the new content renders. However, If I then deploy a subsequent update needRefresh
is never set to true
. I can see in the Safari dev tools that the Webview does in fact download the new service worker. This is working in Safari desktop.
I'm using the virtual React module and I have code like this
export const App: FC = () => {
const [registrationSW, setRegistrationSW] =
useState<ServiceWorkerRegistration>();
const {
needRefresh: [needRefresh, setNeedRefresh],
updateServiceWorker
} = useRegisterSW({ onRegistered: setRegistrationSW });
// for testing purposes I'm pulling for updates every 10 seconds
useTimer({
callback: () => registrationSW?.update(),
interval: TEN_SECONDS,
running: Boolean(registrationSW) && !needRefresh
});
return (
<>
{needRefresh && (
<UpdateNotification
dismiss={() => setNeedRefresh(false)}
update={() => updateServiceWorker(true)}
/>
)}
{/* ... */}
</>
);
};
If I manually reload the Webview I see the new content. This is my plugin config
pluginPWA({ manifest: false })
Testing device: iOS Simulator
iOS version: 15.2
Browser: WKWebview
I'm willing to investigate this more but I need to put this on pause at the moment.
This is also working in Mobile Safari within the iOS simulator.
@daniel-nagy maybe the update
method is not working/implemented and that's why needRefresh
will never be true, refreshing the page works, so it seems there is a problem with the update
(check also the registration is not undefined on the onRegistered
callback)
It works the first time I update the service worker but If I deploy a 2nd service worker update it does not work. I'm wondering if location.reload
is behaving differently in WKWebview.
@daniel-nagy maybe, here the window.location.reload()
calls:
- for prompt,
updateServiceWorker
logic: https://github.com/antfu/vite-plugin-pwa/blob/main/src/client/build/register.ts#L32 - for auto update: https://github.com/antfu/vite-plugin-pwa/blob/main/src/client/build/register.ts#L55
I'm not sure what changed on my end but WKWebview is working fine now. I see the update notification every time I deploy a SW update.