Skip to content

Commit

Permalink
add service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
bates64 committed Oct 17, 2022
1 parent 7b7837a commit 1af0a63
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 1 deletion.
13 changes: 12 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,16 @@
"alphabetize": { "order": "asc", "caseInsensitive": true }
}
]
}
},
"overrides": [
{
"files": ["service-worker.js"],
"rules": {
"no-restricted-globals": "off"
},
"env": {
"serviceworker": true
}
}
]
}
1 change: 1 addition & 0 deletions mamar-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"dependencies": {
"@adobe/react-spectrum": "^3.21.2",
"@parcel/service-worker": "^2.7.0",
"@react-hook/resize-observer": "^1.2.6",
"@spectrum-icons/workflow": "^4.0.2",
"@types/stats.js": "^0.17.0",
Expand Down
1 change: 1 addition & 0 deletions mamar-web/src/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as WasmBridge from "mamar-wasm-bridge"
import * as React from "react"
import * as ReactDOM from "react-dom/client"

import "../service-worker-load.js"
import report from "./analytics"
import App from "./App"

Expand Down
1 change: 1 addition & 0 deletions mamar-web/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ <h2>Contributing</h2>
</main>

<script type="module">
import "./service-worker-load.js"
import report from "./app/analytics"
report()
</script>
Expand Down
1 change: 1 addition & 0 deletions mamar-web/src/service-worker-load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
navigator.serviceWorker.register(new URL("./service-worker.js", import.meta.url), { type: "module" })
33 changes: 33 additions & 0 deletions mamar-web/src/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { manifest, version } from "@parcel/service-worker"

async function install() {
const cache = await caches.open(version)
await cache.addAll(Array.from(new Set(manifest)))
}
addEventListener("install", evt => evt.waitUntil(install()))

async function activate() {
const keys = await caches.keys()
await Promise.all(
keys.map(key => key !== version && caches.delete(key)),
)
}
addEventListener("activate", evt => evt.waitUntil(activate()))

addEventListener("fetch", evt => {
evt.respondWith((async () => {
const r = await caches.match(evt.request)
if (r) {
console.log("[service worker] Cache hit", evt.request.url)
return r
}

console.error("[service worker] Cache miss", evt.request.url)

const response = await fetch(evt.request)
const cache = await caches.open(version)
console.log(`[service worker] Caching new resource: ${evt.request.url}`)
cache.put(evt.request, response.clone())
return response
})())
})
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1862,6 +1862,11 @@
"@parcel/utils" "2.7.0"
nullthrows "^1.1.1"

"@parcel/service-worker@^2.7.0":
version "2.7.0"
resolved "https://registry.yarnpkg.com/@parcel/service-worker/-/service-worker-2.7.0.tgz#b8525b2129cc21e33bc870c398a25cf9f5c31a12"
integrity sha512-EUMQcBtRQagZ4yVH2BxtToDpplLtfhjYqtvcgwVQIk3WkvMvKsTVCQZb9GHWKMBz7RKGdeQhbtZLPO3ywI0lfA==

"@parcel/source-map@^2.0.0":
version "2.1.1"
resolved "https://registry.yarnpkg.com/@parcel/source-map/-/source-map-2.1.1.tgz#fb193b82dba6dd62cc7a76b326f57bb35000a782"
Expand Down

0 comments on commit 1af0a63

Please sign in to comment.