Browse documentation

SwRuntimeManager

Exposes installing, registration, waiting and updateAvailable state and manages service worker updates.

Table of contents

Category

Value

Import

js
import { SwRuntimeManager } from "valyrian.js/sw";

Public signature

ts
new SwRuntimeManager(options?: CreateSwRuntimeOptions): SwRuntimeManager

API form

SwRuntimeManager is a class. Construct it in the browser, subscribe to the states needed by the interface and call init() to register the configured worker.

Options and defaults

ts
type CreateSwRuntimeOptions = {
  swUrl?: string; // "./sw.js"
  scope?: string; // "/"
  strategy?: "prompt-user" | "auto" | "manual"; // "prompt-user"
  runtime?: {
    isNodeJs?: boolean;
    navigator?: Navigator | null;
    window?: Window | null;
  };
};

State and methods

ts
readonly state: {
  updateAvailable: boolean;
  installing: boolean;
  registration: ServiceWorkerRegistration | null;
  waiting: ServiceWorker | null;
}
init(): Promise<this>
applyUpdate(): void
checkForUpdate(): Promise<void>
unregister(): Promise<boolean>
on("registered" | "updateavailable" | "updated" | "error", callback): () => void
off(event, callback): void

Strategies and events

prompt-user waits for the application to call applyUpdate(). auto applies a waiting worker automatically. manual avoids automatic application and reload. controllerchange emits updated and reloads unless the strategy is manual. Registration failures emit error.