index.tsx raw

   1  import { useRegisterSW } from 'virtual:pwa-register/react'
   2  
   3  export default function UpdateNotification() {
   4    useRegisterSW({
   5      onRegisteredSW(_swUrl, r) {
   6        // Check for updates every 60 seconds
   7        if (r) {
   8          setInterval(() => {
   9            r.update()
  10          }, 60 * 1000)
  11        }
  12      },
  13      onRegisterError(error) {
  14        console.error('SW registration error:', error)
  15      }
  16    })
  17  
  18    // With autoUpdate + skipWaiting + clientsClaim, the new SW activates immediately.
  19    // Listen for controller change and reload to pick up new assets.
  20    if (typeof window !== 'undefined' && navigator.serviceWorker) {
  21      navigator.serviceWorker.addEventListener('controllerchange', () => {
  22        window.location.reload()
  23      })
  24    }
  25  
  26    return null
  27  }
  28