-
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.
Test adding a service worked for PWA installing
- Loading branch information
1 parent
3bd79e2
commit 64e6f22
Showing
2 changed files
with
40 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const CACHE_NAME = `v1`; | ||
|
||
// Use the install event to pre-cache all initial resources. | ||
self.addEventListener('install', event => { | ||
event.waitUntil((async () => { | ||
const cache = await caches.open(CACHE_NAME); | ||
cache.addAll([ | ||
'/', | ||
]); | ||
})()); | ||
}); | ||
|
||
self.addEventListener('fetch', event => { | ||
event.respondWith((async () => { | ||
const cache = await caches.open(CACHE_NAME); | ||
|
||
// Get the resource from the cache. | ||
const cachedResponse = await cache.match(event.request); | ||
if (cachedResponse) { | ||
return cachedResponse; | ||
} else { | ||
try { | ||
// If the resource was not in the cache, try the network. | ||
const fetchResponse = await fetch(event.request); | ||
|
||
// Save the resource in the cache and return it. | ||
cache.put(event.request, fetchResponse.clone()); | ||
return fetchResponse; | ||
} catch (e) { | ||
// The network failed. | ||
} | ||
} | ||
})()); | ||
}); |