You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.0 KiB
42 lines
1.0 KiB
2 years ago
|
const app_name = "fuzzyhome"
|
||
2 years ago
|
import { version } from '../package.json'
|
||
2 years ago
|
const app_prefix = "{app_name}_cache"
|
||
|
const cache_name = "sw-{app_prefix}-{version}"
|
||
2 years ago
|
let p = do |s| console.log "{cache_name} {s}"
|
||
|
p "loaded"
|
||
2 years ago
|
|
||
3 years ago
|
let urls = [
|
||
2 years ago
|
'./'
|
||
3 years ago
|
]
|
||
|
|
||
2 years ago
|
self.addEventListener('fetch') do |e|
|
||
2 years ago
|
p "fetch"
|
||
3 years ago
|
def intercept request
|
||
|
if request
|
||
2 years ago
|
p "responding with cache {e.request.url}"
|
||
3 years ago
|
request
|
||
|
else
|
||
2 years ago
|
p "not cached, fetching {e.request.url}"
|
||
3 years ago
|
fetch e.request
|
||
|
e.respondWith(caches.match(e.request.url).then(intercept))
|
||
|
|
||
2 years ago
|
self.addEventListener('install') do |e|
|
||
2 years ago
|
p "install"
|
||
3 years ago
|
def add_urls_to_cache cache
|
||
2 years ago
|
p "adding urls to cache"
|
||
3 years ago
|
cache.addAll urls
|
||
2 years ago
|
skipWaiting!
|
||
3 years ago
|
e.waitUntil(caches.open(cache_name).then(add_urls_to_cache))
|
||
|
|
||
2 years ago
|
self.addEventListener('activate') do |e|
|
||
2 years ago
|
p "activate"
|
||
3 years ago
|
def delete_cached keys
|
||
2 years ago
|
let temp = keys.map! do |key, i|
|
||
2 years ago
|
p "checking cache {key}"
|
||
3 years ago
|
if key !== cache_name
|
||
2 years ago
|
p "deleting cache {key}"
|
||
|
let result = await caches.delete key
|
||
|
p "deletion of {key} result: {result}"
|
||
3 years ago
|
Promise.all(temp)
|
||
|
e.waitUntil(caches.keys().then(delete_cached))
|