@@ -134,87 +134,77 @@ self.addEventListener("fetch", (event) => {
134
134
request . headers . get ( "Accept" ) . includes ( "text/html" )
135
135
) {
136
136
event . respondWith (
137
- new Promise ( ( resolveWithResponse ) => {
138
- const timer = setTimeout ( ( ) => {
139
- // Time out: CACHE
140
- retrieveFromCache . then ( ( responseFromCache ) => {
141
- if ( responseFromCache ) {
142
- resolveWithResponse ( responseFromCache ) ;
143
- }
144
- } ) ;
137
+ ( async ( ) => {
138
+ // CHECK CACHE
139
+ const timer = setTimeout ( async ( ) => {
140
+ const responseFromCache = await retrieveFromCache ;
141
+ if ( responseFromCache ) {
142
+ return responseFromCache ;
143
+ }
145
144
} , timeout ) ;
146
145
147
- Promise . resolve ( event . preloadResponse )
148
- . then ( ( preloadResponse ) => preloadResponse || fetch ( request ) )
149
- . then ( ( responseFromPreloadOrFetch ) => {
150
- // NETWORK
151
- clearTimeout ( timer ) ;
152
- const copy = responseFromPreloadOrFetch . clone ( ) ;
153
- // Stash a copy of this page in the pages cache
154
- try {
155
- event . waitUntil (
156
- caches . open ( pagesCacheName ) . then ( ( pagesCache ) => {
157
- return pagesCache . put ( request , copy ) ;
158
- } ) ,
159
- ) ;
160
- } catch ( error ) {
161
- console . error ( error ) ;
162
- }
163
- resolveWithResponse ( responseFromPreloadOrFetch ) ;
164
- } )
165
- . catch ( ( preloadOrFetchError ) => {
166
- clearTimeout ( timer ) ;
167
- console . error ( preloadOrFetchError , request ) ;
168
- // CACHE or FALLBACK
169
- retrieveFromCache . then ( ( responseFromCache ) => {
170
- resolveWithResponse (
171
- responseFromCache || caches . match ( "/offline" ) ,
172
- ) ;
173
- } ) ;
174
- } ) ;
175
- } ) ,
146
+ try {
147
+ const preloadResponse = await Promise . resolve ( event . preloadResponse ) ;
148
+ const responseFromPreloadOrFetch =
149
+ preloadResponse || ( await fetch ( request ) ) ;
150
+
151
+ // NETWORK
152
+ // Save a copy of page to pages cache
153
+ clearTimeout ( timer ) ;
154
+ const copy = responseFromPreloadOrFetch . clone ( ) ;
155
+ const pagesCache = await caches . open ( pagesCacheName ) ;
156
+ await pagesCache . put ( request , copy ) ;
157
+
158
+ return responseFromPreloadOrFetch ;
159
+ } catch ( error ) {
160
+ console . error ( error , request ) ;
161
+
162
+ // CACHE or OFFLINE PAGE
163
+ clearTimeout ( timer ) ;
164
+ const responseFromCache = await retrieveFromCache ;
165
+ return responseFromCache || caches . match ( "/offline" ) ;
166
+ }
167
+ } ) ( ) ,
176
168
) ;
169
+
177
170
return ;
178
171
}
179
172
180
173
// For non-HTML requests, look in cache first, fall back to network
181
174
event . respondWith (
182
- retrieveFromCache . then ( ( responseFromCache ) => {
183
- // CACHE
184
- return (
185
- responseFromCache ||
186
- fetch ( request )
187
- . then ( ( responseFromFetch ) => {
188
- // NETWORK
189
- // If request is for an image, stash copy in image cache
190
- if ( / \. ( j p e ? g | p n g | g i f | s v g | w e b p ) / . test ( request . url ) ) {
191
- const copy = responseFromFetch . clone ( ) ;
192
- try {
193
- event . waitUntil (
194
- caches . open ( imageCacheName ) . then ( ( imagesCache ) => {
195
- return imagesCache . put ( request , copy ) ;
196
- } ) ,
197
- ) ;
198
- } catch ( error ) {
199
- console . error ( error ) ;
200
- }
201
- }
202
- return responseFromFetch ;
203
- } )
204
- . catch ( ( fetchError ) => {
205
- console . error ( fetchError ) ;
206
- // FALLBACK
207
- // Show an offline placeholder
208
- if ( / \. ( j p e ? g | p n g | g i f | s v g | w e b p ) / . test ( request . url ) ) {
209
- return new Response ( placeholderImage , {
210
- headers : {
211
- "Content-Type" : "image/svg+xml" ,
212
- "Cache-Control" : "no-store" ,
213
- } ,
214
- } ) ;
215
- }
216
- } )
217
- ) ;
218
- } ) ,
175
+ ( async ( ) => {
176
+ try {
177
+ const responseFromCache = await retrieveFromCache ;
178
+
179
+ if ( responseFromCache ) {
180
+ // CACHE
181
+ return responseFromCache ;
182
+ } else {
183
+ const responseFromFetch = await fetch ( request ) ;
184
+
185
+ // NETWORK
186
+ // If request is for an image, save a copy to images cache
187
+ if ( / \. ( j p e ? g | p n g | g i f | s v g | w e b p ) / . test ( request . url ) ) {
188
+ const copy = responseFromFetch . clone ( ) ;
189
+ const imagesCache = await caches . open ( imageCacheName ) ;
190
+ await imagesCache . put ( request , copy ) ;
191
+ }
192
+
193
+ return responseFromFetch ;
194
+ }
195
+ } catch ( error ) {
196
+ console . error ( error ) ;
197
+
198
+ // OFFLINE IMAGE
199
+ if ( / \. ( j p e ? g | p n g | g i f | s v g | w e b p ) / . test ( request . url ) ) {
200
+ return new Response ( placeholderImage , {
201
+ headers : {
202
+ "Content-Type" : "image/svg+xml" ,
203
+ "Cache-Control" : "no-store" ,
204
+ } ,
205
+ } ) ;
206
+ }
207
+ }
208
+ } ) ( ) ,
219
209
) ;
220
210
} ) ;
0 commit comments