14
14
* limitations under the License.
15
15
**/
16
16
17
+ import { CacheableResponsePlugin } from 'workbox-cacheable-response' ;
Has a conversation. Original line has a conversation.
18
+ import { CacheFirst , StaleWhileRevalidate } from 'workbox-strategies' ;
19
+ import { cleanupOutdatedCaches , matchPrecache , precacheAndRoute }
20
+ from 'workbox-precaching' ;
21
+ import { clientsClaim , skipWaiting } from 'workbox-core' ;
22
+ import { ExpirationPlugin } from 'workbox-expiration' ;
23
+ import { registerRoute } from 'workbox-routing' ;
24
+ import { strategy as streamsStrategy } from 'workbox-streams' ;
25
+
17
26
import { API_CACHE_NAME , DEFAULT_TAG } from './lib/constants.mjs' ;
18
27
import * as templates from './lib/templates.mjs' ;
19
28
import * as urls from './lib/urls.mjs' ;
20
29
import partials from './lib/partials.mjs' ;
21
30
import routeMatchers from './lib/route-matchers.mjs' ;
22
31
23
- importScripts ( 'https://storage.googleapis.com/workbox-cdn/releases/4.0.0-beta.1/workbox-sw.js' ) ;
24
- workbox . setConfig ( {
25
- debug : true ,
26
- } ) ;
27
- workbox . precaching . precacheAndRoute ( [ ] ) ;
28
- workbox . precaching . cleanupOutdatedCaches ( ) ;
29
-
30
- const cacheStrategy = new workbox . strategies . CacheFirst ( {
31
- cacheName : workbox . core . cacheNames . precache ,
32
- } ) ;
32
+ precacheAndRoute ( self . __WB_MANIFEST ) ;
Has a conversation. Original line has a conversation.
33
+ cleanupOutdatedCaches ( ) ;
33
34
34
- const apiStrategy = new workbox . strategies . StaleWhileRevalidate ( {
35
+ const apiStrategy = new StaleWhileRevalidate ( {
35
36
cacheName : API_CACHE_NAME ,
36
37
plugins : [
37
- new workbox . expiration . Plugin ( { maxEntries : 50 } ) ,
38
+ new ExpirationPlugin ( { maxEntries : 50 } ) ,
Has a conversation. Original line has a conversation. 38
39
] ,
39
40
} ) ;
40
41
41
- workbox . routing . registerRoute (
42
+ registerRoute (
42
43
routeMatchers . get ( 'about' ) ,
43
- workbox . streams . strategy ( [
44
- ( ) => cacheStrategy . makeRequest ( { request : partials . head ( ) } ) ,
45
- ( ) => cacheStrategy . makeRequest ( { request : partials . navbar ( ) } ) ,
46
- ( ) => cacheStrategy . makeRequest ( { request : partials . about ( ) } ) ,
47
- ( ) => cacheStrategy . makeRequest ( { request : partials . foot ( ) } ) ,
44
+ streamsStrategy ( [
45
+ ( ) => matchPrecache ( partials . head ) ,
Has a conversation. Original line has a conversation.
46
+ ( ) => matchPrecache ( partials . navbar ) ,
47
+ ( ) => matchPrecache ( partials . about ) ,
48
+ ( ) => matchPrecache ( partials . foot ) ,
48
49
] )
49
50
) ;
50
51
51
- workbox . routing . registerRoute (
52
+ registerRoute (
52
53
routeMatchers . get ( 'questions' ) ,
53
- workbox . streams . strategy ( [
54
- ( ) => cacheStrategy . makeRequest ( { request : partials . head ( ) } ) ,
55
- ( ) => cacheStrategy . makeRequest ( { request : partials . navbar ( ) } ) ,
56
- async ( { event, url , params} ) => {
54
+ streamsStrategy ( [
55
+ ( ) => matchPrecache ( partials . head ) ,
56
+ ( ) => matchPrecache ( partials . navbar ) ,
57
+ async ( { event, params} ) => {
57
58
try {
58
59
const questionId = params [ 1 ] ;
59
- const questionResponse = await apiStrategy . makeRequest ( {
60
+ const questionResponse = await apiStrategy . handle ( {
Has a conversation. Original line has a conversation. 60
61
event,
61
62
request : urls . getQuestion ( questionId ) ,
62
63
} ) ;
@@ -66,19 +67,19 @@ workbox.routing.registerRoute(
66
67
return templates . error ( error . message ) ;
67
68
}
68
69
} ,
69
- ( ) => cacheStrategy . makeRequest ( { request : partials . foot ( ) } ) ,
70
+ ( ) => matchPrecache ( partials . foot ) ,
70
71
] )
71
72
) ;
72
73
73
- workbox . routing . registerRoute (
74
+ registerRoute (
74
75
routeMatchers . get ( 'index' ) ,
75
- workbox . streams . strategy ( [
76
- ( ) => cacheStrategy . makeRequest ( { request : partials . head ( ) } ) ,
77
- ( ) => cacheStrategy . makeRequest ( { request : partials . navbar ( ) } ) ,
76
+ streamsStrategy ( [
77
+ ( ) => matchPrecache ( partials . head ) ,
78
+ ( ) => matchPrecache ( partials . navbar ) ,
78
79
async ( { event, url} ) => {
79
80
try {
80
81
const tag = url . searchParams . get ( 'tag' ) || DEFAULT_TAG ;
81
- const listResponse = await apiStrategy . makeRequest ( {
82
+ const listResponse = await apiStrategy . handle ( {
82
83
event,
83
84
request : urls . listQuestionsForTag ( tag ) ,
84
85
} ) ;
@@ -88,37 +89,37 @@ workbox.routing.registerRoute(
88
89
return templates . error ( error . message ) ;
89
90
}
90
91
} ,
91
- ( ) => cacheStrategy . makeRequest ( { request : partials . foot ( ) } ) ,
92
+ ( ) => matchPrecache ( partials . foot ) ,
92
93
] )
93
94
) ;
94
95
95
96
// Gravatar images support CORS, so we won't be storing opaque responses.
96
- workbox . routing . registerRoute (
97
+ registerRoute (
97
98
new RegExp ( 'https://www\\.gravatar\\.com/' ) ,
98
- new workbox . strategies . CacheFirst ( {
99
+ new CacheFirst ( {
99
100
cacheName : 'profile-images' ,
100
101
plugins : [
101
- new workbox . expiration . Plugin ( {
102
+ new ExpirationPlugin ( {
102
103
maxEntries : 50 ,
103
104
purgeOnQuotaError : true ,
104
105
} ) ,
105
106
] ,
106
107
} )
107
108
) ;
108
109
109
- workbox . routing . registerRoute (
110
+ registerRoute (
110
111
new RegExp ( '^https://.*(?:\\.jpg|\\.png)' ) ,
111
- new workbox . strategies . CacheFirst ( {
112
+ new CacheFirst ( {
112
113
cacheName : 'other-images' ,
113
114
plugins : [
114
- new workbox . cacheableResponse . Plugin ( { statuses : [ 0 , 200 ] } ) ,
115
- new workbox . expiration . Plugin ( {
115
+ new CacheableResponsePlugin ( { statuses : [ 0 , 200 ] } ) ,
116
+ new ExpirationPlugin ( {
116
117
maxEntries : 10 ,
117
118
purgeOnQuotaError : true ,
118
119
} ) ,
119
120
] ,
120
121
} )
121
122
) ;
122
123
123
- workbox . core . skipWaiting ( ) ;
124
- workbox . core . clientsClaim ( ) ;
124
+ skipWaiting ( ) ;
125
+ clientsClaim ( ) ;