-
-
Notifications
You must be signed in to change notification settings - Fork 241
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
1 parent
5a09f2b
commit de0d80e
Showing
3 changed files
with
51 additions
and
32 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* eslint-disable no-restricted-globals */ | ||
|
||
// Files to cache | ||
const cacheName = "regexlearn-pwa-v1"; | ||
const appShellFiles = ["/", "/static/"]; | ||
|
||
const contentToCache = appShellFiles; | ||
|
||
// Installing Service Worker | ||
self.addEventListener("install", (e) => { | ||
console.log("[Service Worker] Install"); | ||
e.waitUntil( | ||
(async () => { | ||
const cache = await caches.open(cacheName); | ||
console.log("[Service Worker] Caching all: app shell and content"); | ||
await cache.addAll(contentToCache); | ||
})() | ||
); | ||
}); | ||
|
||
// Fetching content using Service Worker | ||
self.addEventListener("fetch", (e) => { | ||
e.respondWith( | ||
(async () => { | ||
const r = await caches.match(e.request); | ||
console.log(`[Service Worker] Fetching resource: ${e.request.url}`); | ||
if (r) return r; | ||
const response = await fetch(e.request); | ||
const cache = await caches.open(cacheName); | ||
console.log(`[Service Worker] Caching new resource: ${e.request.url}`); | ||
if (!/^https?:$/i.test(new URL(e.request.url).protocol)) return null; | ||
cache.put(e.request, response.clone()); | ||
return response; | ||
})() | ||
); | ||
}); |