Service Worker

Table of Contents

References

Exploration

js
return navigator
txtUpdated 143w ago
{}
js
if ('serviceWorker' in navigator) {
  return true
}
txtUpdated 143.6w ago
true
jsregister
return navigator
          .serviceWorker
          .register('/serviceworker.js?root='+lit.location.root)
txtUpdated 134.7w ago
{}
  • To do, handle register for non-root hosted notebooks.
jsexec=onloadstatus
return navigator.serviceWorker.controller
         ? "Service worker active."
         : "Service worker Not active."
jsunregister
return (async fn => {
  const regs = await navigator
                     .serviceWorker
                     .getRegistrations()
  for(let registration of regs) {
    console.log(registration)
    registration.unregister()
  }
  return `Unregistered ${regs.length} regisration(s)`
})()
txtUpdated 68.7w ago
{}
Unregistered 1 regisration(s)
txtUpdated 85.6w ago
{}
Unregistered 1 regisration(s)
js
return fetch('/none')
       .then( resp => resp.status )
txtUpdated 142.8w ago
404
js
return fetch('/manifest.json')
       .then(resp => resp.status)
txtUpdated 143w ago
200
js
return (new Response('hello')).text()
txtUpdated 143.6w ago
hello

As of now, the service worker doesn't cache anything and just passes through to the network as normal. Except if the request.url ends with --sw in which case it returns a mock/info response.

js
return fetch('/--sw')
       .then(resp => resp.text()
                          .then( text => `${resp.status}\n${text}`)
       )
txtUpdated 136.8w ago
200
{
  "version": "0.2.17",
  "dotlit": "object",
  "root": "/",
  "enableCache": false,
  "count": 11,
  "filepath": "/",
  "stat": {
    "type": "dir",
    "mode": 511,
    "size": 0,
    "ino": 0,
    "mtimeMs": 1621892330665,
    "ctimeMs": 1621892330665,
    "uid": 1,
    "gid": 1,
    "dev": 1
  }
}
text< ./getserviceworker--sw

Implementation

To do

  • Serve from cache first for specified domains (skypack, unpkg, jsdeliver etc)
  • Add responses not on host domain to RUNTIME cache for offline usage...?