Commit 7e31e461 authored by Michał 'rysiek' Woźniak's avatar Michał 'rysiek' Woźniak
Browse files

working on making event.request available where we need it

parent 688f22eb
......@@ -29,7 +29,7 @@ let getContentFromCache = (url) => {
* or a string containing a URL
* or an Array of string URLs
*/
let cacheContent = (resource, url) => {
let cacheContent = (resource, key) => {
return caches.open('v1')
.then((cache) => {
if (typeof resource === 'string') {
......@@ -42,15 +42,16 @@ let cacheContent = (resource, url) => {
return cache.addAll(resource)
} else {
// assume a Response
// which means we either have URL in url, or in resource.url
if (typeof url !== 'string' || url === '') {
// which means we either have a Request in key, a string URL in key,
// or we can use the URL in resource.url
if ( (typeof key !== 'object') && ( (typeof key !== 'string') || (key === '') ) ) {
if (typeof resource.url !== 'string' || resource.url === '') {
throw new Error('No URL to work with!')
}
url = resource.url
key = resource.url
}
console.log("(COMMIT_UNKNOWN) caching a Response: " + url)
return cache.put(url, resource)
console.log("(COMMIT_UNKNOWN) caching a Response to: " + key)
return cache.put(key, resource)
}
})
}
......
......@@ -243,13 +243,17 @@ let callOnSamizdatPlugin = (call, args) => {
* and returns a Promise resolving to a Response in case any of the plugins
* was able to get the resource
*
* url - string containing the URL we want to fetch
* request - string containing the URL we want to fetch
* clientId - string containing the clientId of the requesting client
* useStashed - use stashed resources; if false, only pull resources from live sources
* doStash - stash resources once fetched successfully; if false, do not stash pulled resources automagically
* stashedResponse - TBD
*/
let getResourceThroughSamizdat = (url, clientId, useStashed=true, doStash=true) => {
let getResourceThroughSamizdat = (request, clientId, useStashed=true, doStash=true, stashedResponse=null) => {
// clean the URL, removing any fragment identifier
var url = event.request.url.replace(/#.+$/, '');
// set-up reqInfo for the fetch event
var reqInfo = new SamizdatResourceInfo(url, clientId)
......@@ -318,29 +322,46 @@ let getResourceThroughSamizdat = (url, clientId, useStashed=true, doStash=true)
console.log('(COMMIT_UNKNOWN) starting background no-stashed fetch for:', url);
// event.waitUntil?
// https://stackoverflow.com/questions/37902441/what-does-event-waituntil-do-in-service-worker-and-why-is-it-needed/37906330#37906330
getResourceThroughSamizdat(url, clientId, false, true)
getResourceThroughSamizdat(request, clientId, false, true, response.clone())
// return the response so that stuff can keep happening
return response
// otherwise, let's see if we want to stash
} else if (doStash) {
// find the first stashing plugin
for (i=0; i<SamizdatPlugins.length; i++) {
if (typeof SamizdatPlugins[i].stash === 'function') {
// ok, now we're in business
console.log('(COMMIT_UNKNOWN) Stashing a successful fetch of:', url,
'\n+-- fetched using :', reqInfo.method,
'\n+-- stashing using :', SamizdatPlugins[i].name)
// working on clone()'ed response so that the original one is not touched
// TODO: should a failed stashing break the flow here? probably not!
return SamizdatPlugins[i].stash(response.clone(), url)
.then((res)=>{
// original response will be needed further down
return response
// and if we already had a stashed version that differs
} else {
// do we have a stashed version that differs?
if (typeof stashedResponse === "object") {
// this is where we check if the response from whatever plugin we got it from
// is newer than what we've stashed
}
// do we want to stash?
if (doStash) {
// find the first stashing plugin
for (i=0; i<SamizdatPlugins.length; i++) {
if (typeof SamizdatPlugins[i].stash === 'function') {
// ok, now we're in business
console.log('(COMMIT_UNKNOWN) Stashing a successful fetch of:', url,
'\n+-- fetched using :', reqInfo.method,
'\n+-- stashing using :', SamizdatPlugins[i].name)
new Request(request, {
headers: new Headers({
'X-Samizdat-Method': reqInfo.method,
'X-Samizdat-'
})
})
// working on clone()'ed response so that the original one is not touched
// TODO: should a failed stashing break the flow here? probably not!
return SamizdatPlugins[i].stash(response.clone(), url)
.then((res)=>{
// original response will be needed further down
return response
})
}
}
}
}
......@@ -435,12 +456,9 @@ self.addEventListener('fetch', event => {
return void event.respondWith(fetch(event.request));
}
// clean the URL, removing any fragment identifier
var cleanURL = event.request.url.replace(/#.+$/, '');
// GET requests to our own domain that are *not* #samizdat-info requests
// get handled by plugins in case of an error
return void event.respondWith(getResourceThroughSamizdat(cleanURL, clientId))
return void event.respondWith(getResourceThroughSamizdat(event.request, clientId))
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment