Browse documentation

registerSw

Registers a service worker file for the application.

Table of contents

Category

Value

Import

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

Public signature

ts
registerSw(file?: string, options?: RegistrationOptions, navigatorRef?: Navigator | null): Promise<ServiceWorkerRegistration | undefined>

API form

registerSw is an asynchronous function. It registers one service worker and returns its registration.

Defaults and result

ts
file = "./sw.js"
options = { scope: "/" }
navigatorRef = globalThis.navigator

Promise<ServiceWorkerRegistration | undefined>

Unavailable runtime

When the selected navigator has no serviceWorker API, registerSw resolves undefined without registering a worker. Registration failures reject the returned promise.