All files / src/lib service-worker.ts

100% Statements 5/5
66.66% Branches 6/9
100% Functions 2/2
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20                4x 4x 2x     2x 1x          
// Registers the service worker (public/sw.js), which makes Kotivalo
// installable and shows an offline page. Only in a build: in `pnpm dev` it
// would keep Vite's files and get in the way.
import { clientLog } from './client-log'
 
export function registerServiceWorker(
  options: { enabled?: boolean } = {},
): void {
  const enabled = options.enabled ?? import.meta.env.PROD
  if (!enabled || !('serviceWorker' in navigator)) {
    return
  }
  // Some browsers refuse it, e.g. in private windows; the app works anyway.
  navigator.serviceWorker.register('/sw.js').catch((error: unknown) => {
    clientLog.warn('app.service_worker_failed', {
      reason: error instanceof Error ? error.name : 'unknown',
    })
  })
}