From 418124de5c51c4e8c1a8f0921a12a5a9f0193951 Mon Sep 17 00:00:00 2001 From: Tixie Date: Thu, 14 Oct 2021 16:39:01 +0200 Subject: [PATCH] fix(service worker): Add new Parcel build-in feature of SW --- package-lock.json | 5 +++++ package.json | 1 + service-worker.js | 15 +++++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 service-worker.js diff --git a/package-lock.json b/package-lock.json index 585e2b1..5f57c2d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1004,6 +1004,11 @@ "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": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/@parcel/source-map/-/source-map-2.0.0.tgz", diff --git a/package.json b/package.json index 8383c93..9675f1a 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "lint-fix": "eslint --ext .js,vue . --fix" }, "dependencies": { + "@parcel/service-worker": "^2.0.0", "folktale": "^2.3.2", "generic-pool": "^3.7.1", "gif-writer": "^0.9.3", diff --git a/service-worker.js b/service-worker.js new file mode 100644 index 0000000..26545f4 --- /dev/null +++ b/service-worker.js @@ -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()))