All files / app-play/src/service-worker register.ts

33.33% Statements 3/9
60% Branches 3/5
25% Functions 1/4
37.5% Lines 3/8

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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35            1x     1x               4x                                  
// Registers the player's service worker (`service-worker.ts`, served as `/sw.js`; docs/player-web.md, "Installing the
// player"). Only a production build registers it: the dev server has no `/sw.js`. Runs under the test hook
// (`?e2e=1`) do not either, so the end-to-end suite's request routes see every request.
 
import { logger } from '@be-music/player-browser';
 
const log = logger('service-worker');
 
/** The worker's URL and scope: the player at the origin's root. The embed page is excluded by the worker's rules. */
export const SERVICE_WORKER_URL = '/sw.js';
 
/** Whether this page registers the service worker. */
export function shouldRegisterServiceWorker(options: {
  production: boolean;
  testHook: boolean;
  supported: boolean;
}): boolean {
  return options.production && !options.testHook && options.supported;
}
 
/**
 * Registers the worker once the page has loaded, so its install (precaching the shell) does not compete with the
 * first start. `updateViaCache: 'none'` makes every update check fetch `/sw.js` past the HTTP cache.
 */
export function registerServiceWorker(container: ServiceWorkerContainer): void {
  const register = () => {
    container.register(SERVICE_WORKER_URL, { scope: '/', updateViaCache: 'none' }).catch((error: unknown) => {
      // The player works without it; only installing and offline starts are lost.
      log.warn('registration failed', error);
    });
  };
  if (document.readyState === 'complete') register();
  else window.addEventListener('load', register, { once: true });
}