Created by
Hazel Campbell (hazel.campbell@ualberta.ca).
Copyright 2023.
navigator.serviceWorker
object, a ServiceWorkerContainer
if ("serviceWorker" in navigator) {
navigator.serviceWorker... // do something with the ServiceWorkerContainer API
}
const registerServiceWorker = async () => {
if ("serviceWorker" in navigator) {
await navigator.serviceWorker... // do something with the ServiceWorkerContainer API
}
}
// just start the function and proceed with the rest of the script
registerServiceWorker();
const sendToAllTabs async (data) => {
(await self.clients.matchAll()).map((client) => {
// data is some structured-cloneable object
await client.postMessage(data);
});
}
if ("serviceWorker" in navigator) {
navigator.serviceWorker.addEventListener("message", (event) => {
if ("data" in event) {
// data.event is the structured-cloneable message from the serviceworker
}
})
navigator.serviceWorker.startMessages();
}
copy.deepcopy
event.waitUntil(somePromise)
event.waitUntil()
to ask browser for more timeself.skipWaiting()
in install
event handlerclients.claim()
in activate
event handlerinstall
- service worker is first installedactivate
- service worker becomes the active service worker versionmessage
- a tab (client) sent the serviceworker a messagefetch
- page makes a fetch requestpush...
- Push API (Notifications)self.addEventListener("fetch", (event) => { ... });
event.respondWith(promise for response)
to take over handling the requestrespondWith
to let the browser handle as normalRequest
and decide if it should be handled by SWfetch()
to supply resultinstall
event handlerconst fillCache = async () => {
const cache = await caches.open(cache_version);
await cache.addAll(['./', 'index.html', 'somepage.html', 'somecode.js', 'somestyle.css', 'somepicture.avif']);
}
const cacheKeys = await caches.keys();
for (const cacheKey of cacheKeys) {
if (cacheKey !== cache_version) {
await caches.delete(cacheKey);
}
}
Copyright 2014-2023 ⓒ Abram Hindle
Copyright 2019-2023 ⓒ Hazel Victoria Campbell and contributors
Other images used under fair use and copyright their copyright holders.
Copyright (C) 2019-2023 Hazel Victoria Campbell
Copyright (C) 2014-2023 Abram Hindle and contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN.
01234567890123456789012345678901234567890123456789012345678901234567890123456789