Browse Source

clean up service worker

main
familyfriendlymikey 2 years ago
parent
commit
f4e5a68ff2
  1. 2
      package.json
  2. 35
      sw.imba

2
package.json

@ -1,6 +1,6 @@
{ {
"name": "fuzzyhome", "name": "fuzzyhome",
"version": "0.1.0", "version": "0.1.1",
"scripts": { "scripts": {
"start": "imba run -SMH --baseurl . -w server.imba", "start": "imba run -SMH --baseurl . -w server.imba",
"build-app": "rm -rf dist && imba build -SMH --baseurl . server.imba", "build-app": "rm -rf dist && imba build -SMH --baseurl . server.imba",

35
sw.imba

@ -1,8 +1,6 @@
let p = console.log let p = console.log
const app_prefix = "fuzzyhome_cache" const app_prefix = "fuzzyhome_cache"
const version = "0.1.0" const version = "0.1.1"
const cache_name = "{app_prefix}-{version}" const cache_name = "{app_prefix}-{version}"
let urls = [ let urls = [
@ -12,39 +10,30 @@ let urls = [
'./__assets__/app/client.css' './__assets__/app/client.css'
] ]
self.addEventListener('fetch', &) do |e| self.addEventListener('fetch') do |e|
def intercept request def intercept request
if request if request
p `{cache_name} responding with cache : {e.request.url}` p "{cache_name} responding with cache : {e.request.url}"
request request
else else
p `{cache_name} not cached, fetching : {e.request.url}` p "{cache_name} not cached, fetching : {e.request.url}"
fetch e.request fetch e.request
e.respondWith(caches.match(e.request.url).then(intercept)) e.respondWith(caches.match(e.request.url).then(intercept))
self.addEventListener('install', &) do |e| self.addEventListener('install') do |e|
p `{cache_name} install` p "{cache_name} install"
def add_urls_to_cache cache def add_urls_to_cache cache
p `{cache_name} installing cache : {cache_name}` p "{cache_name} installing cache : {cache_name}"
cache.addAll urls cache.addAll urls
skipWaiting! # [TODO]: Export this and import to settings component and have manual update button skipWaiting!
e.waitUntil(caches.open(cache_name).then(add_urls_to_cache)) e.waitUntil(caches.open(cache_name).then(add_urls_to_cache))
self.addEventListener('activate') do |e|
self.addEventListener('activate', &) do |e| p "{cache_name} activate"
p `{cache_name} activate`
def delete_cached keys def delete_cached keys
let temp = keys.map! do |key, i|
let temp = keys.map(&) do |key, i|
if key !== cache_name if key !== cache_name
p `{cache_name} deleting cache : {keys[i]}` p "{cache_name} deleting cache : {keys[i]}"
caches.delete keys[i] caches.delete keys[i]
Promise.all(temp) Promise.all(temp)
e.waitUntil(caches.keys().then(delete_cached)) e.waitUntil(caches.keys().then(delete_cached))

Loading…
Cancel
Save