Skip to content

Commit

Permalink
replace registerSWWithOptions with onRegisteredSWCheckForUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Nov 19, 2023
1 parent 7deab8c commit 95839cd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 56 deletions.
28 changes: 28 additions & 0 deletions src/util/serviceWorker/onRegisteredSWCheckForUpdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { setIntervalAsync } from 'set-interval-async'

/**
* Check for new app version, see https://vite-pwa-org.netlify.app/guide/periodic-sw-updates.html
* @param checkForNewVersionsIntervalSeconds Interval to check for new application versions
*/
export default function onRegisteredSWCheckForUpdate(swScriptUrl : string, registration : ServiceWorkerRegistration|undefined,
checkForNewVersionsIntervalSeconds: number) {
const checkForNewVersionsIntervalMilliseconds = checkForNewVersionsIntervalSeconds * 1000
registration && setIntervalAsync(async () => {
if (!(!registration.installing && navigator)) {
return
}
if (('connection' in navigator) && !navigator.onLine) {
return
}
const resp = await fetch(swScriptUrl, {
cache: 'no-store',
headers: {
'cache': 'no-store',
'cache-control': 'no-cache',
}
})
if (resp?.status === 200) {
await registration.update()
}
}, checkForNewVersionsIntervalMilliseconds)
}
56 changes: 0 additions & 56 deletions src/util/serviceWorker/registerSWWithOptions.ts

This file was deleted.

0 comments on commit 95839cd

Please sign in to comment.