fix(service worker): Add new Parcel build-in feature of SW

This commit is contained in:
Tixie 2021-10-14 16:39:01 +02:00
parent 0133a60458
commit 418124de5c
3 changed files with 21 additions and 0 deletions

5
package-lock.json generated
View file

@ -1004,6 +1004,11 @@
"nullthrows": "^1.1.1" "nullthrows": "^1.1.1"
} }
}, },
"@parcel/service-worker": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/@parcel/service-worker/-/service-worker-2.0.0.tgz",
"integrity": "sha512-fqFY2a/Y+ajbPiTQIriqeOG/dOJP6Ci8fWwcm1+hf5+QFm/PHcOHme9XL1PBVMPyfpGkE9h1VjK4F9mzV1I8Tg=="
},
"@parcel/source-map": { "@parcel/source-map": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/@parcel/source-map/-/source-map-2.0.0.tgz", "resolved": "https://registry.npmjs.org/@parcel/source-map/-/source-map-2.0.0.tgz",

View file

@ -11,6 +11,7 @@
"lint-fix": "eslint --ext .js,vue . --fix" "lint-fix": "eslint --ext .js,vue . --fix"
}, },
"dependencies": { "dependencies": {
"@parcel/service-worker": "^2.0.0",
"folktale": "^2.3.2", "folktale": "^2.3.2",
"generic-pool": "^3.7.1", "generic-pool": "^3.7.1",
"gif-writer": "^0.9.3", "gif-writer": "^0.9.3",

15
service-worker.js Normal file
View file

@ -0,0 +1,15 @@
import { manifest, version } from '@parcel/service-worker'
async function install () {
const cache = await caches.open(version)
await cache.addAll(manifest)
}
addEventListener('install', e => e.waitUntil(install()))
async function activate () {
const keys = await caches.keys()
await Promise.all(
keys.map(key => key !== version && caches.delete(key))
)
}
addEventListener('activate', e => e.waitUntil(activate()))