chromium/third_party/blink/web_tests/wpt_internal/service-worker/static-router/resources/static-router-sw.js

'use strict';

import {routerRules} from './router-rules.js';
import {
  recordRequest,
  recordError,
  getRecords,
  resetRecords } from './static-router-sw.sub.js';

self.addEventListener('install', async e => {
  const params = new URLSearchParams(location.search);
  const key = params.get('key');
  try {
    await e.addRoutes(routerRules[key]);
  } catch (e) {
    recordError(e);
  }
  self.skipWaiting();
});

self.addEventListener('activate', e => {
  e.waitUntil(clients.claim());
});

self.addEventListener('fetch', function(event) {
  recordRequest(event.request);
  const url = new URL(event.request.url);
  const nonce = url.searchParams.get('nonce');
  event.respondWith(new Response(nonce));
});

self.addEventListener('message', function(event) {
  if (event.data.reset) {
    resetRecords();
  }
  if (event.data.port) {
    const {requests, errors} = getRecords();
    event.data.port.postMessage({requests, errors});
  }
});