-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
})()) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters