Skip to content

Commit 3ffd52b

Browse files
Use cache.addAll
1 parent c154e97 commit 3ffd52b

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

_posts/2023-03-13-this-blog-is-now-a-pwa.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ If suddenly the device connects to the network it automatically reloads the page
3535

3636
---
3737

38-
I did hit this snag when using [`cache.addAll`] to cache my offline page. It does cache everything correctly. But when the user is offline and tries to navigate somewhere and the worker returns the offline page this error occurs:
38+
I did hit this snag when using [`cache.addAll`] to cache my offline page. It does cache everything correctly. But when the user is offline and tries to navigate somewhere and the worker returns the cached offline page this error occurs:
3939

4040
> The FetchEvent for "http://page-the-user-was-trying-to-access"
4141
> resulted in a network error response: a redirected response was
4242
> used for a request whose redirect mode is not "follow".
4343
44-
Apparently this is due to a [new security restriction] but I don't get it. It all seems so cryptic to me. [StackOverflow came to my rescue] though. So now I'm just calling `fetch` for each of the pages that I want to cache. All tutorials that I saw did use [`cache.addAll`] so either I'm doing something wrong or they're already out of date. If you bump into the same problem now you know!
44+
Apparently this is due to a [new security restriction] but I don't get it. It all seems so cryptic to me. [StackOverflow came to my rescue] though. So now I'm wrapping a `response` with `new Response(response.body)`. All tutorials that I saw simply used [`cache.addAll`] so either I'm doing something wrong or they're already out of date. If you bump into the same problem now you know!
4545

4646
---
4747

service-worker.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
var CACHE_NAME = 'v1';
22

3-
function cacheResources(urls) {
3+
function cacheInitialResources() {
44
return caches.open(CACHE_NAME).then(cache =>
5-
Promise.all(urls.map((url) => cache.add(url)))
5+
cache.addAll([
6+
'/',
7+
'/blog/',
8+
'/offline',
9+
])
610
);
711
}
812

@@ -52,13 +56,7 @@ function fetchOrGoToOfflinePage(fetchEvent) {
5256

5357
function onInstall(installEvent) {
5458
skipWaiting();
55-
installEvent.waitUntil(
56-
cacheResources([
57-
'/',
58-
'/blog/',
59-
'/offline',
60-
])
61-
);
59+
installEvent.waitUntil(cacheInitialResources());
6260
}
6361

6462
function onActivate(activateEvent) {

0 commit comments

Comments
 (0)