Module serviceworker
Service workers (src/page/service_worker.odin; native/js/runtime/web_sw.c): registrations kept by the browser, each with up to three versions (installing, waiting, active), each version a JavaScript worker hosted by a page of its own while it runs; requests intercepted by the loader for the pages it controls. Page side: register, ready, the registrations and their versions, update, unregister, navigation preload, the controller, messages. Worker side: the events the browser sends (install, activate, fetch, message, the answers to its requests) and what the worker does. ServiceWorkerRegistration_info and ServiceWorker_info answer in both realms (a service worker reads the last state the browser sent it).
Exposed to: Window, Worker. Source: api/serviceworker.widl.
Odin: import "wren:serviceworker".
Types
struct SwEventInfo
What the current event of a service worker's realm is (kind 0: none waits).
| field | type | |
|---|---|---|
kind | i32 | 1 install, 2 activate, 3 fetch, 4 message, 5 the answer to a request (ServiceWorkerGlobalScope_reply 10), 6 the registration changed (ServiceWorkerRegistration_info has its new state), 7 a navigation preload's response |
id | u64 | fetch / preload: the fetch's id; answer: the request's id |
mode | i32 | fetch: the request's mode (fetch.Mode); answer: 0 done, else rejected (text 0: why); preload: the status (0: failed) |
destination | i32 | fetch: the request's destination (fetch.Destination) |
client | u64 | the client (a page's id), 0 for a navigation |
flags | i32 | fetch: 1 when a navigation preload runs for it (FetchEvent.preloadResponse) |
Functions
ServiceWorker_register
Promise ServiceWorker_register(string script, optional string scope, string update_via_cache, bool module) raisesnavigator.serviceWorker.register(script, {scope, updateViaCache, type}): a promise of the registration's id, resolved once the script ran (an unchanged script: at once; TypeError when it failed). Security outside a secure context, for another origin or a scope outside what the script may control.
Odin: serviceworker.register.
Raises: returns an Error (see web).
Host: ServiceWorker_register :: proc(c: ^Call, script: string, scope: Maybe(string), update_via_cache: string, module: bool) -> (u64, Api_Error)
ServiceWorker_ready
Promise ServiceWorker_ready()navigator.serviceWorker.ready: a promise of the id of the active registration whose scope holds the page.
Odin: serviceworker.ready.
Host: ServiceWorker_ready :: proc(c: ^Call) -> u64
ServiceWorker_getRegistration
u64 ServiceWorker_getRegistration(string client_url)getRegistration(clientURL): the id of the registration matching the URL (0: none).
Odin: serviceworker.getRegistration.
Host: ServiceWorker_getRegistration :: proc(c: ^Call, client_url: string) -> u64
ServiceWorker_registrationIds
string ServiceWorker_registrationIds()getRegistrations(): the ids of the origin's registrations, "\n"-separated.
Odin: serviceworker.registrationIds.
Host: ServiceWorker_registrationIds :: proc(c: ^Call) -> string
ServiceWorker_controller
u64 ServiceWorker_controller()navigator.serviceWorker.controller: the id of the version controlling the page (0: none).
Odin: serviceworker.controller.
Host: ServiceWorker_controller :: proc(c: ^Call) -> u64
ServiceWorkerRegistration_info
string ServiceWorkerRegistration_info(u64 id)A registration ("" when it is gone), "\n"-separated: scope, updateViaCache, the installing, waiting and active versions' ids (0: none), navigation preload enabled (1 / 0), its header value.
Host: ServiceWorkerRegistration_info :: proc(c: ^Call, id: u64) -> string
ServiceWorker_info
string ServiceWorker_info(u64 version)A version ("" when unknown): its state, then its script URL ("\n"-separated).
Odin: serviceworker.info.
Host: ServiceWorker_info :: proc(c: ^Call, version: u64) -> string
ServiceWorkerRegistration_unregisterId
bool ServiceWorkerRegistration_unregisterId(u64 id)registration.unregister(): true when it was the origin's and is gone.
Host: ServiceWorkerRegistration_unregisterId :: proc(c: ^Call, id: u64) -> bool
ServiceWorkerRegistration_updateId
Promise ServiceWorkerRegistration_updateId(u64 id) raisesregistration.update(): a promise of the registration's id once its script was checked (a new version then installs). Invalid_State when the registration is gone.
Raises: returns an Error (see web).
Host: ServiceWorkerRegistration_updateId :: proc(c: ^Call, id: u64) -> (u64, Api_Error)
ServiceWorkerRegistration_preload
string ServiceWorkerRegistration_preload(u64 id, i32 op, string header) raisesregistration.navigationPreload: op 0 getState, 1 enable, 2 disable, 3 setHeaderValue(header). The state after: enabled (1 / 0), "\n", the header value. Invalid_State for enable / disable without an active version.
Raises: returns an Error (see web).
Host: ServiceWorkerRegistration_preload :: proc(c: ^Call, id: u64, op: i32, header: string) -> (string, Api_Error)
ServiceWorker_postToWorker
void ServiceWorker_postToWorker(u64 version, bytes data) raisesserviceWorker.postMessage(message): the serialized message to that version's worker (started if it stopped).
Odin: serviceworker.postToWorker.
Raises: returns an Error (see web).
Host: ServiceWorker_postToWorker :: proc(c: ^Call, version: u64, data: []u8) -> Api_Error
ServiceWorker_takeMessage
bytes ServiceWorker_takeMessage() raisesThe next message a service worker sent this page (Not_Found when none waits): the sender's version id (8 bytes) then the serialized message.
Odin: serviceworker.takeMessage.
Raises: returns an Error (see web).
Host: ServiceWorker_takeMessage :: proc(c: ^Call) -> ([]u8, Api_Error)
ServiceWorkerGlobalScope_registration
u64 ServiceWorkerGlobalScope_registration()In a service worker's realm: its registration's id (0 in any other realm), its version's id, scope, script URL.
Host: ServiceWorkerGlobalScope_registration :: proc(c: ^Call) -> u64
ServiceWorkerGlobalScope_version
u64 ServiceWorkerGlobalScope_version()Host: ServiceWorkerGlobalScope_version :: proc(c: ^Call) -> u64
ServiceWorkerGlobalScope_scope
string ServiceWorkerGlobalScope_scope()Host: ServiceWorkerGlobalScope_scope :: proc(c: ^Call) -> string
ServiceWorkerGlobalScope_scriptURL
string ServiceWorkerGlobalScope_scriptURL()Host: ServiceWorkerGlobalScope_scriptURL :: proc(c: ^Call) -> string
SwEvent_next
SwEventInfo SwEvent_next()The current event; its strings (0 URL / an answer's text, 1 method, 2 header lines), its body; done: the next one.
Host: SwEvent_next :: proc(c: ^Call) -> Api_SwEventInfo
SwEvent_text
string SwEvent_text(i32 which)Host: SwEvent_text :: proc(c: ^Call, which: i32) -> string
SwEvent_body
bytes SwEvent_body()Host: SwEvent_body :: proc(c: ^Call) -> []u8
SwEvent_done
void SwEvent_done()Host: SwEvent_done :: proc(c: ^Call)
ServiceWorkerGlobalScope_reply
void ServiceWorkerGlobalScope_reply(i32 kind, u64 id, i64 code, string headers, string text, bytes body)What the worker did: 1 install done (code 1 ok, 0 failed), 2 activate done, 3 respond (code = status, headers, text = the response's URL, body), 4 fall back to the network, 5 fail (text: why), 6 skipWaiting, 7 clients.claim, 8 a message to a client (code = the client's id, 0 every controlled page; body = the serialized message), 10 a request answered later by an event 5 (id = the request's id, code = 1 clients.matchAll (headers: "includeUncontrolled TAB type"), 2 clients.get (text: id), 3 clients.openWindow (text: URL), 4 navigate (text: URL, headers: the client's id), 5 focus (text: the client's id), 6 registration.update, 7 registration.unregister), 11 navigation preload (code = op as ServiceWorkerRegistration_preload, text = the header value).
Host: ServiceWorkerGlobalScope_reply :: proc(c: ^Call, kind: i32, id: u64, code: i64, headers: string, text: string, body: []u8)