registerServiceWorker()
Register the app's service worker, sw.js at the app's base (import.meta.env.BASEURL), scoped to that base. Waits for the page's load event first, so the worker's precaching never competes with the page's own start-up.
function registerServiceWorker(opts?: RegisterServiceWorkerOptions): Promise<ServiceWorkerRegistration | null>;Defined in: engine/src/pwa/index.ts:41
Does nothing and resolves null:
- on a Vite dev server (
import.meta.env.DEV), which has no worker (the plugin runs onvite buildonly); - on
localhost,127.0.0.1or[::1], unless RegisterServiceWorkerOptions.localhost is set, so local previews, smoke tests and benches never serve from a cache; - where the browser has no service workers (or the page is not a secure context).
Otherwise it resolves the registration, or null after onError when registering failed
(e.g. a build without the plugin, so no sw.js).
Parameters
| Parameter | Type |
|---|---|
opts? | RegisterServiceWorkerOptions |
Returns
Promise<ServiceWorkerRegistration | null>
Example
registerServiceWorker({
onOfflineReady: () => console.info("ready to work offline"),
onUpdate: () => showReloadHint(), // the app's own UI, if it wants one
});