I’d like to only have the service worker update check occur eg 5 mins into the app being used.
Problem is that because it’s wrapped in
window.addEventListener('load', async () => {
navigator.serviceWorker.register("/serviceWorker.js");
const registration = await navigator.serviceWorker.getRegistration();
it starts its checks at load time.
To get around it I put the service worker init in a function that I can call whenever I want, but of course, it fails because…
window.addEventListener('load', async ()
was called after the window was loaded.
What’s the general idea for delaying service worker checks?
3
Answers
you can use setTimeout
You’ll want to use
setTimeout()
like this:The first parameter to
setTimeout()
is a callback that is called after the delay, and the second parameter is that delay in milliseconds (5 minutes = 300,000 milliseconds).Also, as Arman Charan pointed out, if instead of waiting you want to immediately get the registration, then just await
navigator.serviceWorker.register
:I would consider
await
ingServiceWorkerContainer.register()
.The following
registerSingletonServiceWorkerUrls
implementation demonstrates how to register service worker URLs and retrieve the resultingServiceWorkerRegistration
as needed usingPromise
s.