@@ -9,7 +9,7 @@ const shouldConserveData = () => {
9
9
10
10
if ( connection . saveData )
11
11
return true ;
12
-
12
+
13
13
return connection . type === "cellular" ;
14
14
} ;
15
15
@@ -31,28 +31,28 @@ const downloadFirstRunAssets = async () => {
31
31
] ) ;
32
32
} ;
33
33
34
- self . addEventListener ( 'install' , function ( e ) {
34
+ self . addEventListener ( 'install' , function ( e ) {
35
35
console . log ( '[ServiceWorker] Install' ) ;
36
36
e . waitUntil ( downloadFirstRunAssets ( )
37
37
. then ( ( ) => self . skipWaiting ( ) ) ) ;
38
38
} ) ;
39
39
40
- self . addEventListener ( 'activate' , function ( e ) {
40
+ self . addEventListener ( 'activate' , function ( e ) {
41
41
console . log ( '[ServiceWorker] Activate' ) ;
42
42
return self . clients . claim ( ) ;
43
43
} ) ;
44
44
45
45
const cache = async ( request , response ) => {
46
46
if ( response . then )
47
47
response = await response ;
48
-
48
+
49
49
const copy = response . clone ( ) ;
50
-
50
+
51
51
if ( copy . ok ) {
52
52
const store = await caches . open ( CACHE_NAME ) ;
53
53
await store . put ( request , copy ) ;
54
54
}
55
-
55
+
56
56
// Return the response, to make this thenable.
57
57
return response ;
58
58
}
@@ -63,37 +63,37 @@ const networkThenCache = async e => {
63
63
. catch ( ( ) => caches . match ( e . request ) ) ;
64
64
}
65
65
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 ) ;
68
68
if ( cached )
69
69
return cached ;
70
-
70
+
71
71
return fetch ( e . request ) . then ( r => cache ( e . request , r ) ) ;
72
72
}
73
73
74
74
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 ) ;
77
77
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 ( ) ) ;
80
80
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 ( ) ) ) ;
83
83
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 ) ;
89
89
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 ) ;
93
93
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 ] ) ;
97
97
} ;
98
98
99
99
@@ -103,15 +103,18 @@ const maybeConserve = (normal, conservative) => {
103
103
} ;
104
104
105
105
// Map regexes to a strategy.
106
- const rules = {
106
+ const rules = {
107
107
// First party scripts should be fetched from the network, if possible.
108
108
[ self . registration . scope ] : networkThenCache ,
109
109
110
110
// Fetch latest mountain data, if we have a network connection.
111
111
"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
112
115
}
113
116
114
- self . addEventListener ( 'fetch' , function ( e ) {
117
+ self . addEventListener ( 'fetch' , function ( e ) {
115
118
for ( const rule in rules ) {
116
119
const regex = new RegExp ( rule ) ;
117
120
if ( regex . test ( e . request . url ) ) {
0 commit comments