Created
May 12, 2018 08:05
-
-
Save okjodom/de8937dfa1d0ba247bbf3c2c0f504430 to your computer and use it in GitHub Desktop.
Attempt to serve avatars from cache then update cache
This file contains hidden or 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
| ... | |
| function serveAvatar(request) { | |
| // Avatar urls look like: | |
| // avatars/sam-2x.jpg | |
| // But storageUrl has the -2x.jpg bit missing. | |
| // Use this url to store & match the image in the cache. | |
| // This means you only store one copy of each avatar. | |
| var storageUrl = request.url.replace(/-\dx\.jpg$/, ''); | |
| // TODO: return images from the "wittr-content-imgs" cache | |
| // if they're in there. But afterwards, go to the network | |
| // to update the entry in the cache. | |
| return caches.open(contentImgsCache).then(cache => { | |
| return cache.match(storageUrl).then(response => { | |
| return response; | |
| }).then( response => { | |
| if (!response) { | |
| return fetch(request).then(networkResponse => { | |
| cache.put(storageUrl, networkResponse.copy()); | |
| return networkResponse; | |
| }); | |
| } | |
| fetch(request).then(networkResponse => { | |
| cache.put(storageUrl, networkResponse); | |
| }); | |
| }); | |
| }); | |
| // Note that this is slightly different to servePhoto! | |
| } | |
| ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment