Skip to content

Commit e57e02d

Browse files
committed
[sw] proxy with cache-first to ys.static
1 parent 3a555da commit e57e02d

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

sw.js

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const DEPRECATED_CACHES = ['precache-v1', 'runtime', 'main-precache-v1', 'main-r
4040

4141

4242
// The Util Function to hack URLs of intercepted requests
43-
const getFixedUrl = (req) => {
43+
const getCacheBustingUrl = (req) => {
4444
var now = Date.now();
4545
url = new URL(req.url)
4646

@@ -129,6 +129,33 @@ self.addEventListener('activate', event => {
129129
});
130130

131131

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+
132159
/**
133160
* @Functional Fetch
134161
* All network requests are being intercepted here.
@@ -150,14 +177,19 @@ self.addEventListener('fetch', event => {
150177
return;
151178
}
152179

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
154187
// similar to HTTP's stale-while-revalidate: https://www.mnot.net/blog/2007/12/12/stale
155188
// Upgrade from Jake's to Surma's: https://gist.github.com/surma/eb441223daaedf880801ad80006389f1
156189
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" });
159191
const fetchedCopy = fetched.then(resp => resp.clone());
160-
192+
161193
// Call respondWith() with whatever we get first.
162194
// Promise.race() resolves with first one settled (even rejected)
163195
// If the fetch fails (e.g disconnected), wait for the cache.
@@ -213,6 +245,8 @@ function sendMessageToClientsAsync(msg) {
213245

214246
/**
215247
* 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+
*
216250
* @param {Promise<response>} cachedResp [description]
217251
* @param {Promise<response>} fetchedResp [description]
218252
* @return {Promise}

0 commit comments

Comments
 (0)