-
Notifications
You must be signed in to change notification settings - Fork 1
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
6 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,47 @@ | ||
import { version, manifest } from "@parcel/service-worker"; | ||
|
||
declare const self: ServiceWorkerGlobalScope; | ||
|
||
async function install() { | ||
const cache = await caches.open(version); | ||
await cache.addAll(manifest); | ||
await cache.add("/"); | ||
} | ||
|
||
self.addEventListener("install", (e: ExtendableEvent) => { | ||
e.waitUntil(install()); | ||
self.skipWaiting(); | ||
}); | ||
|
||
async function activate() { | ||
const keys = await caches.keys(); | ||
await Promise.all( | ||
keys.map(key => key !== version && caches.delete(key)) | ||
); | ||
} | ||
|
||
self.addEventListener("activate", (e: ExtendableEvent) => { | ||
e.waitUntil(activate()); | ||
e.waitUntil(self.clients.claim()); | ||
}); | ||
|
||
self.addEventListener("fetch", (e: FetchEvent) => { | ||
if (e.request.method !== "GET") return; | ||
|
||
async function getResponse() { | ||
const cache = await caches.open(version); | ||
|
||
// Parcel adds stupid timestamps to the end of the URLs (at least for debug builds) | ||
// and I can't figure out how to disable them, | ||
// so I'm just going to strip them off here | ||
const realUrl = e.request.url.replace(/\?.*$/, ""); | ||
const cachedResponse = await cache.match(realUrl); | ||
if (cachedResponse) { | ||
return cachedResponse; | ||
} | ||
|
||
return fetch(e.request); | ||
} | ||
|
||
e.respondWith(getResponse()); | ||
}); |
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,17 @@ | ||
export async function init() { | ||
if (!("serviceWorker" in navigator)) { | ||
console.error("Service workers are not supported in this browser."); | ||
} | ||
|
||
try { | ||
await navigator.serviceWorker.register( | ||
new URL("ServiceWorker.ts", import.meta.url), | ||
{ | ||
scope: "/", | ||
type: "module" | ||
} | ||
); | ||
} catch (e) { | ||
console.error("Service worker registration failed:", e); | ||
} | ||
} |
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,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": ["ES2015", "WebWorker"], | ||
}, | ||
"include": [ | ||
"src/AudioWorker.ts", | ||
"src/ServiceWorker.ts", | ||
] | ||
} |