Web APIs
ServiceWorkerContainer.prototype.register()
Registers or updates a service worker for a scope on an eligible origin; resolves to a registration.
Formal Syntax & Structure
javascript syntaxjavascript
navigator.serviceWorker.register(scriptURL, [options])Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| scriptURL | string | URL | Service worker script URL. |
| options | object | Scope and other registration options. |
Return Value
Promise<ServiceWorkerRegistration>
Registers or updates a service worker for a scope on an eligible origin; resolves to a registration.
Working Example
Example Snippetjavascript
(async () => {const r=await navigator.serviceWorker.register("./sw.js",{scope:"./"});console.log(r.scope===new URL("./",location.href).href);})()