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
   36
   37
   38
   39
   40
   41
   42
   43

content / test / data / service_worker / create_service_worker.html [blame]

<html>
<title>create service worker</title>
<script>
async function register(worker_url, scope, type) {
  try {
    const init = {};
    if (scope)
      init['scope'] = scope;
    if (type)
      init['type'] = type;
    await navigator.serviceWorker.register(worker_url, init);
    await navigator.serviceWorker.ready;
    return 'DONE';
  } catch (error) {
    return `${error}`;
  }
}

async function registerWithoutAwaitingReady(worker_url, scope) {
  try {
    const init = scope ? {scope} : {};
    await navigator.serviceWorker.register(worker_url, init);
    // Don't await for navigator.serviceWorker.ready.
    return 'DONE';
  } catch (error) {
    return `${error}`;
  }
}

async function update(scope) {
  try {
    const registration =
        scope ?
            await navigator.serviceWorker.getRegistration(scope) :
            await navigator.serviceWorker.ready;
    await registration.update();
    return 'DONE';
  } catch (error) {
    return `${error}`;
  }
}
</script>
</html>