service-worker
Observes navigator.serviceWorker and optionally relays its events — zero app JS required.
Features
- Observation only Reports on an existing Service Worker; never registers one
- Support detection
is-supportedreflects API availability - Ready state
is-readyreflects once an active worker controls the page - Event relay
relay-eventsforwardsmessage/messageerror/controllerchangeas plain DOM events - Bindable state
.provisionis{ isSupported, isReady, hasController, scope }— kept current on connect,ready, and everycontrollerchange; read it from Quark withprop("provision")
Installation
This package is available in the
<script src="https://unpkg.com/@excom/kit-utils@0.3.0/dist/index.umd.min.js"></script>
<script src="https://unpkg.com/@excom/neutron@0.2.0/dist/index.umd.min.js"></script>
<script src="https://unpkg.com/@excom/service-worker@0.1.4/dist/index.umd.min.js"></script>npm install @excom/service-worker<!-- import path to `node_modules` will depend on your build setup -->
<script type="module" src="/node_modules/@excom/service-worker"></script>
<link rel="stylesheet" href="/node_modules/@excom/service-worker">import "@excom/service-worker";Peer dependencies (0)
Packages a consumer must install alongside this one. Workspace deps are bundled.
| Package | Version |
|---|---|
|
Usage
This element only observes an already-registered Service Worker — it does not call navigator.serviceWorker.register(...) itself. Register your Service Worker separately (in app code, or your build tool), then drop this element anywhere to expose its state as attributes and, optionally, relay its events.
<event-handler listen-for="message" fire-event="sw-message-received">
<service-worker relay-events="message"></service-worker>
</event-handler><event-handler listen-for="message" fire-event="sw-message-received">
<service-worker relay-events="message"></service-worker>
</event-handler>Relayed events (message, messageerror, controllerchange) are dispatched with their original names — they are not prefixed with service-worker-.
API Reference
Attributes
Role —option is configurable, state
is managed by the element (read-only), or hybrid which is both.
Provision
Thisprovision property lives on the DOM node.
Read / watch it with Quark’s prop("provision"), or listen
for the neutron-provision event from app JS.
Events
Type
command event (HTML Command API): from a
<button command commandfor>, an <event-handler
command-name>, or a CommandEvent.
Recognized Elements
Child or descendant elements are recognized by and relevant to its functionality.Styles
@custom-selector synonyms so custom elements can share
semantic styles. Element aliases match the host tag / .tag-* class; state
aliases match attributes (nested under the element alias).
service-worker
Attributes (3)
Role —option is configurable, state
is managed by the element (read-only), or hybrid which is both.
relay-events
navigator.serviceWorker events to relay onto this element. Bare attribute (no value) relays all three. Event names are relayed as-is — not prefixed with the tag.
relayEvents
tokenlist
"message" | "messageerror" | "controllerchange"nullis-ready
navigator.serviceWorker.ready has resolved — an active worker is controlling the page.
isReady
boolean
falseis-supported
'serviceWorker' in navigator).
isSupported
boolean
falseProvision (1)
Thisprovision property lives on the DOM node.
Read / watch it with Quark’s prop("provision"), or listen
for the neutron-provision event from app JS.
provision
{ isSupported, isReady, hasController, scope } — set on connect, when ready resolves (scope comes from the registration), and on every controllerchange. Not reflected as an attribute.
provision
ServiceWorkerProvision
Events (3)
e.preventDefault() is not
synchronously called on the event.
Type
controllerchange
navigator.serviceWorker's controllerchange event when controllerchange is included in relay-events. Not tag-prefixed.
Type
ServiceWorkerControllerChangeEvent
message
navigator.serviceWorker's message event when message is included in relay-events. Not tag-prefixed. event.detail is event.data from the original message.
Type
ServiceWorkerMessageEvent
messageerror
navigator.serviceWorker's messageerror event when messageerror is included in relay-events. Not tag-prefixed.
Type
ServiceWorkerMessageErrorEvent
command event (HTML Command API): from a
<button command commandfor>, an <event-handler
command-name>, or a CommandEvent.
Recognized Elements (0)
Child or descendant elements are recognized by service-worker and relevant to its functionality.Styles (0)
all: revert-layer.
Valence.css ships this as .unstyled and .unstyled-all for the entire tree.
@custom-selector synonyms so custom elements can share
semantic styles. Element aliases match the host tag / .tag-* class; state
aliases match attributes (nested under the element alias).
Release notes (1)
0.1.1
- Ship only dist (and declared extras) in the npm tarball; drop build logs, tests and sources
Release notes
e.preventDefault() is not
synchronously called on the event.
all: revert-layer.
Valence.css ships this as .unstyled and .unstyled-all for the entire tree.
Examples
Support / mount / ready state
is-supported, is-mounted (reflected by default), and is-ready are all plain attributes — style or branch on them with CSS. is-ready needs an app-registered Service Worker to ever resolve, and this docs site registers one, so it is set here.
Relay messages from your Service Worker
Relaying message / messageerror / controllerchange requires a Service Worker that your app has already registered and that is actively posting messages — this is not runnable in this docs site, but works like so once wired up:
<service-worker relay-events="message"></service-worker>
<script>
document
.querySelector("service-worker")
.addEventListener("message", (e) => console.log(e.detail));
</script><service-worker relay-events="message"></service-worker>
<script>
document
.querySelector("service-worker")
.addEventListener("message", (e) => console.log(e.detail));
</script>