-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 changed files
with
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
if ("serviceWorker" in navigator) { | ||
addEventListener("load", function () { | ||
navigator.serviceWorker | ||
// we're going to give it scope / so it needs to be in the root of the repo | ||
.register("/sw.js") | ||
.then(() => console.log("service worker registered")) | ||
.catch((err) => console.log("service worker not registered", err)); | ||
}); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,12 @@ | ||
{ | ||
"name": "My App", | ||
"start_url": "/", | ||
"display": "standalone", | ||
"icons": [ | ||
{ | ||
"src": "/pwa/favicon.png", | ||
"type": "image/png", | ||
"sizes": "256x256" | ||
} | ||
] | ||
} |
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,24 @@ | ||
// Cache requests so the app can work offline | ||
// https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps/Offline_Service_workers | ||
const cacheName = "myapp"; | ||
|
||
self.addEventListener("fetch", (e) => { | ||
e.respondWith( | ||
(async () => { | ||
try { | ||
const response = await fetch(e.request); | ||
const cache = await caches.open(cacheName); | ||
console.log(`[Service Worker] Caching resource: ${e.request.url}`); | ||
cache.put(e.request, response.clone()); | ||
return response; | ||
} catch { | ||
// if we have no network access, try to use the cache | ||
const response = await caches.match(e.request); | ||
console.log(`[Service Worker] Using cached resource: ${e.request.url}`); | ||
if (response) { | ||
return response; | ||
} | ||
} | ||
})(), | ||
); | ||
}); |