@@ -40,7 +40,7 @@ const DEPRECATED_CACHES = ['precache-v1', 'runtime', 'main-precache-v1', 'main-r
40
40
41
41
42
42
// The Util Function to hack URLs of intercepted requests
43
- const getFixedUrl = ( req ) => {
43
+ const getCacheBustingUrl = ( req ) => {
44
44
var now = Date . now ( ) ;
45
45
url = new URL ( req . url )
46
46
@@ -129,6 +129,33 @@ self.addEventListener('activate', event => {
129
129
} ) ;
130
130
131
131
132
+ var fetchHelper = {
133
+
134
+ fetchThenCache : function ( request ) {
135
+ // Requests with mode "no-cors" can result in Opaque Response,
136
+ // Requests to Allow-Control-Cross-Origin: * can't include credentials.
137
+ const init = { mode : "cors" , credentials : "omit" }
138
+
139
+ const fetched = fetch ( request , init )
140
+ const fetchedCopy = fetched . then ( resp => resp . clone ( ) ) ;
141
+
142
+ // NOTE: Opaque Responses have no hedaders so [[ok]] make no sense to them
143
+ // so Opaque Resp will not be cached in this case.
144
+ Promise . all ( [ fetchedCopy , caches . open ( CACHE ) ] )
145
+ . then ( ( [ response , cache ] ) => response . ok && cache . put ( request , response ) )
146
+ . catch ( _ => { /* eat any errors */ } )
147
+
148
+ return fetched ;
149
+ } ,
150
+
151
+ cacheFirst : function ( url ) {
152
+ return caches . match ( url )
153
+ . then ( resp => resp || this . fetchThenCache ( url ) )
154
+ . catch ( _ => { /* eat any errors */ } )
155
+ }
156
+ }
157
+
158
+
132
159
/**
133
160
* @Functional Fetch
134
161
* All network requests are being intercepted here.
@@ -150,14 +177,19 @@ self.addEventListener('fetch', event => {
150
177
return ;
151
178
}
152
179
153
- // Stale-while-revalidate
180
+ // Cache-only Startgies for ys.static resources
181
+ if ( event . request . url . indexOf ( 'ys.static' ) > - 1 ) {
182
+ event . respondWith ( fetchHelper . cacheFirst ( event . request . url ) )
183
+ return ;
184
+ }
185
+
186
+ // Stale-while-revalidate for possiblily dynamic content
154
187
// similar to HTTP's stale-while-revalidate: https://www.mnot.net/blog/2007/12/12/stale
155
188
// Upgrade from Jake's to Surma's: https://gist.github.com/surma/eb441223daaedf880801ad80006389f1
156
189
const cached = caches . match ( event . request ) ;
157
- const fixedUrl = getFixedUrl ( event . request ) ;
158
- const fetched = fetch ( fixedUrl , { cache : "no-store" } ) ;
190
+ const fetched = fetch ( getCacheBustingUrl ( event . request ) , { cache : "no-store" } ) ;
159
191
const fetchedCopy = fetched . then ( resp => resp . clone ( ) ) ;
160
-
192
+
161
193
// Call respondWith() with whatever we get first.
162
194
// Promise.race() resolves with first one settled (even rejected)
163
195
// If the fetch fails (e.g disconnected), wait for the cache.
@@ -213,6 +245,8 @@ function sendMessageToClientsAsync(msg) {
213
245
214
246
/**
215
247
* if content modified, we can notify clients to refresh
248
+ * TODO: Gh-pages rebuild everything in each release. should find a workaround (e.g. ETag with cloudflare)
249
+ *
216
250
* @param {Promise<response> } cachedResp [description]
217
251
* @param {Promise<response> } fetchedResp [description]
218
252
* @return {Promise }
0 commit comments