1
1
const cacheVersion = 'v1.11.1' ;
2
2
const cacheTitle = `pairdrop-cache-${ cacheVersion } ` ;
3
- const forceFetch = false ; // FOR DEVELOPMENT: Set to true to always update assets instead of using cached versions
4
3
const relativePathsToCache = [
5
4
'./' ,
6
5
'index.html' ,
7
6
'manifest.json' ,
8
7
'styles/styles-main.css' ,
9
8
'styles/styles-deferred.css' ,
9
+ 'scripts/browser-tabs-connector.js' ,
10
10
'scripts/localization.js' ,
11
11
'scripts/main.js' ,
12
12
'scripts/network.js' ,
@@ -28,45 +28,57 @@ const relativePathsToCache = [
28
28
'images/android-chrome-512x512.png' ,
29
29
'images/android-chrome-512x512-maskable.png' ,
30
30
'images/apple-touch-icon.png' ,
31
+ 'fonts/OpenSans/static/OpenSans-Medium.ttf' ,
31
32
'lang/ar.json' ,
32
33
'lang/be.json' ,
34
+ 'lang/bg.json' ,
33
35
'lang/ca.json' ,
34
36
'lang/cs.json' ,
35
37
'lang/da.json' ,
36
38
'lang/de.json' ,
37
39
'lang/en.json' ,
38
40
'lang/es.json' ,
41
+ 'lang/et.json' ,
42
+ 'lang/eu.json' ,
43
+ 'lang/fa.json' ,
39
44
'lang/fr.json' ,
40
45
'lang/he.json' ,
41
46
'lang/hu.json' ,
42
47
'lang/id.json' ,
43
48
'lang/it.json' ,
44
49
'lang/ja.json' ,
45
50
'lang/kn.json' ,
51
+ 'lang/ko.json' ,
46
52
'lang/nb.json' ,
47
53
'lang/nl.json' ,
54
+ 'lang/nn.json' ,
48
55
'lang/pl.json' ,
49
56
'lang/pt-BR.json' ,
50
57
'lang/ro.json' ,
51
58
'lang/ru.json' ,
59
+ 'lang/sk.json' ,
60
+ 'lang/ta.json' ,
52
61
'lang/tr.json' ,
53
62
'lang/uk.json' ,
54
63
'lang/zh-CN.json' ,
64
+ 'lang/zh-HK.json' ,
55
65
'lang/zh-TW.json'
56
66
] ;
57
67
const relativePathsNotToCache = [
58
68
'config'
59
69
]
60
70
61
71
self . addEventListener ( 'install' , function ( event ) {
62
- // Perform install steps
72
+ // Perform install steps
73
+ console . log ( "Cache files for sw:" , cacheVersion ) ;
63
74
event . waitUntil (
64
75
caches . open ( cacheTitle )
65
76
. then ( function ( cache ) {
66
77
return cache
67
78
. addAll ( relativePathsToCache )
68
79
. then ( _ => {
69
- console . log ( 'All files cached.' ) ;
80
+ console . log ( 'All files cached for sw:' , cacheVersion ) ;
81
+ self . skipWaiting ( ) ;
70
82
} ) ;
71
83
} )
72
84
) ;
@@ -76,20 +88,25 @@ self.addEventListener('install', function(event) {
76
88
const fromNetwork = ( request , timeout ) =>
77
89
new Promise ( ( resolve , reject ) => {
78
90
const timeoutId = setTimeout ( reject , timeout ) ;
79
- fetch ( request )
91
+ fetch ( request , { cache : "no-store" } )
80
92
. then ( response => {
93
+ if ( response . redirected ) {
94
+ throw new Error ( "Fetch is redirect. Abort usage and cache!" ) ;
95
+ }
96
+
81
97
clearTimeout ( timeoutId ) ;
82
98
resolve ( response ) ;
83
99
100
+ // Prevent requests that are in relativePathsNotToCache from being cached
84
101
if ( doNotCacheRequest ( request ) ) return ;
85
102
86
- update ( request )
103
+ updateCache ( request )
87
104
. then ( ( ) => console . log ( "Cache successfully updated for" , request . url ) )
88
- . catch ( reason => console . log ( "Cache could not be updated for" , request . url , "Reason:" , reason ) ) ;
105
+ . catch ( err => console . log ( "Cache could not be updated for" , request . url , err ) ) ;
89
106
} )
90
107
. catch ( error => {
91
108
// Handle any errors that occurred during the fetch
92
- console . error ( `Could not fetch ${ request . url } . Are you online? ` ) ;
109
+ console . error ( `Could not fetch ${ request . url } .` ) ;
93
110
reject ( error ) ;
94
111
} ) ;
95
112
} ) ;
@@ -111,16 +128,16 @@ const doNotCacheRequest = request => {
111
128
} ;
112
129
113
130
// cache the current page to make it available for offline
114
- const update = request => new Promise ( ( resolve , reject ) => {
115
- if ( doNotCacheRequest ( request ) ) {
116
- reject ( "Url is specifically prevented from being cached in the serviceworker." ) ;
117
- return ;
118
- }
131
+ const updateCache = request => new Promise ( ( resolve , reject ) => {
119
132
caches
120
133
. open ( cacheTitle )
121
134
. then ( cache =>
122
135
fetch ( request , { cache : "no-store" } )
123
136
. then ( response => {
137
+ if ( response . redirected ) {
138
+ throw new Error ( "Fetch is redirect. Abort usage and cache!" ) ;
139
+ }
140
+
124
141
cache
125
142
. put ( request , response )
126
143
. then ( ( ) => resolve ( ) ) ;
@@ -129,9 +146,10 @@ const update = request => new Promise((resolve, reject) => {
129
146
) ;
130
147
} ) ;
131
148
132
- // general strategy when making a request (eg if online try to fetch it
133
- // from cache, if something fails fetch from network. Update cache everytime files are fetched.
134
- // This way files should only be fetched if cacheVersion is changed
149
+ // general strategy when making a request:
150
+ // 1. Try to retrieve file from cache
151
+ // 2. If cache is not available: Fetch from network and update cache.
152
+ // This way, cached files are only updated if the cacheVersion is changed
135
153
self . addEventListener ( 'fetch' , function ( event ) {
136
154
if ( event . request . method === "POST" ) {
137
155
// Requests related to Web Share Target.
@@ -141,39 +159,48 @@ self.addEventListener('fetch', function(event) {
141
159
} ) ( ) ) ;
142
160
}
143
161
else {
144
- // Regular requests not related to Web Share Target.
145
- if ( forceFetch ) {
146
- event . respondWith ( fromNetwork ( event . request , 10000 ) ) ;
147
- }
148
- else {
149
- event . respondWith (
150
- fromCache ( event . request )
162
+ // Regular requests not related to Web Share Target:
163
+ // If request is excluded from cache -> respondWith fromNetwork
164
+ // else -> try fromCache first
165
+ event . respondWith (
166
+ doNotCacheRequest ( event . request )
167
+ ? fromNetwork ( event . request , 10000 )
168
+ : fromCache ( event . request )
151
169
. then ( rsp => {
152
170
// if fromCache resolves to undefined fetch from network instead
153
- return rsp || fromNetwork ( event . request , 10000 ) ;
171
+ if ( ! rsp ) {
172
+ throw new Error ( "No match found." ) ;
173
+ }
174
+ return rsp ;
154
175
} )
155
- ) ;
156
- }
176
+ . catch ( error => {
177
+ console . error ( "Could not retrieve request from cache:" , event . request . url , error ) ;
178
+ return fromNetwork ( event . request , 10000 ) ;
179
+ } )
180
+ ) ;
157
181
}
158
182
} ) ;
159
183
160
184
161
185
// on activation, we clean up the previously registered service workers
162
186
self . addEventListener ( 'activate' , evt => {
163
- return evt . waitUntil (
164
- caches . keys ( )
165
- . then ( cacheNames => {
166
- return Promise . all (
167
- cacheNames . map ( cacheName => {
168
- if ( cacheName !== cacheTitle ) {
169
- return caches . delete ( cacheName ) ;
170
- }
171
- } )
172
- ) ;
173
- } )
174
- )
175
- }
176
- ) ;
187
+ console . log ( "Activate sw:" , cacheVersion ) ;
188
+ evt . waitUntil ( clients . claim ( ) ) ;
189
+ return evt . waitUntil (
190
+ caches
191
+ . keys ( )
192
+ . then ( cacheNames => {
193
+ return Promise . all (
194
+ cacheNames . map ( cacheName => {
195
+ if ( cacheName !== cacheTitle ) {
196
+ console . log ( "Delete cache:" , cacheName ) ;
197
+ return caches . delete ( cacheName ) ;
198
+ }
199
+ } )
200
+ ) ;
201
+ } )
202
+ )
203
+ } ) ;
177
204
178
205
const evaluateRequestData = function ( request ) {
179
206
return new Promise ( async ( resolve ) => {
0 commit comments