Skip to content

Commit ee89fb0

Browse files
Try serve search data from cache
1 parent a9ee97f commit ee89fb0

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

public/serviceWorker.js

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const shouldConserveData = () => {
99

1010
if (connection.saveData)
1111
return true;
12-
12+
1313
return connection.type === "cellular";
1414
};
1515

@@ -31,28 +31,28 @@ const downloadFirstRunAssets = async () => {
3131
]);
3232
};
3333

34-
self.addEventListener('install', function(e) {
34+
self.addEventListener('install', function (e) {
3535
console.log('[ServiceWorker] Install');
3636
e.waitUntil(downloadFirstRunAssets()
3737
.then(() => self.skipWaiting()));
3838
});
3939

40-
self.addEventListener('activate', function(e) {
40+
self.addEventListener('activate', function (e) {
4141
console.log('[ServiceWorker] Activate');
4242
return self.clients.claim();
4343
});
4444

4545
const cache = async (request, response) => {
4646
if (response.then)
4747
response = await response;
48-
48+
4949
const copy = response.clone();
50-
50+
5151
if (copy.ok) {
5252
const store = await caches.open(CACHE_NAME);
5353
await store.put(request, copy);
5454
}
55-
55+
5656
// Return the response, to make this thenable.
5757
return response;
5858
}
@@ -63,37 +63,37 @@ const networkThenCache = async e => {
6363
.catch(() => caches.match(e.request));
6464
}
6565

66-
const cacheThenNetwork = async e => {
67-
const cached = await caches.match(e.response);
66+
const cacheThenNetwork = async e => {
67+
const cached = await caches.match(e.request);
6868
if (cached)
6969
return cached;
70-
70+
7171
return fetch(e.request).then(r => cache(e.request, r));
7272
}
7373

7474
const raceNetworkAndCache = async (e) => {
75-
const cachePromise = caches.match(e.request);
76-
const fetchPromise = fetch(e.request);
75+
const cachePromise = caches.match(e.request);
76+
const fetchPromise = fetch(e.request);
7777

78-
// Gets a promise returning a clone of the fetch request.
79-
const getFetchResponseClone = () => fetchPromise.then(r => r.clone());
78+
// Gets a promise returning a clone of the fetch request.
79+
const getFetchResponseClone = () => fetchPromise.then(r => r.clone());
8080

81-
// Always update what's in the cache whatever it is we tried to fetch.
82-
e.waitUntil(cache(e.request, getFetchResponseClone()));
81+
// Always update what's in the cache whatever it is we tried to fetch.
82+
e.waitUntil(cache(e.request, getFetchResponseClone()));
8383

84-
const responseToReturn = getFetchResponseClone();
85-
// If the cache doesn't have the request, try and fetch it from the network.
86-
const cachedOrNetwork = cachePromise
87-
.then(response => response || responseToReturn)
88-
.catch(() => responseToReturn);
84+
const responseToReturn = getFetchResponseClone();
85+
// If the cache doesn't have the request, try and fetch it from the network.
86+
const cachedOrNetwork = cachePromise
87+
.then(response => response || responseToReturn)
88+
.catch(() => responseToReturn);
8989

90-
// If the network encounters an error, try and return a cached response.
91-
const networkOrCache = responseToReturn
92-
.catch(() => cachePromise);
90+
// If the network encounters an error, try and return a cached response.
91+
const networkOrCache = responseToReturn
92+
.catch(() => cachePromise);
9393

94-
// Promise.race throws an error if either promise rejects, so be careful
95-
// that neither promise can throw.
96-
return Promise.race([cachedOrNetwork, networkOrCache]);
94+
// Promise.race throws an error if either promise rejects, so be careful
95+
// that neither promise can throw.
96+
return Promise.race([cachedOrNetwork, networkOrCache]);
9797
};
9898

9999

@@ -103,15 +103,18 @@ const maybeConserve = (normal, conservative) => {
103103
};
104104

105105
// Map regexes to a strategy.
106-
const rules = {
106+
const rules = {
107107
// First party scripts should be fetched from the network, if possible.
108108
[self.registration.scope]: networkThenCache,
109109

110110
// Fetch latest mountain data, if we have a network connection.
111111
"https://raw.githubusercontent.com/fallaciousreasoning/nz-mountains/main/mountains.json": networkThenCache,
112+
113+
// Use search data from cache.
114+
'https://search.topos.nz/data/min_excluded_places.json': cacheThenNetwork
112115
}
113116

114-
self.addEventListener('fetch', function(e) {
117+
self.addEventListener('fetch', function (e) {
115118
for (const rule in rules) {
116119
const regex = new RegExp(rule);
117120
if (regex.test(e.request.url)) {

0 commit comments

Comments
 (0)