kopia lustrzana https://gitlab.com/rysiekpl/libresilient
rough first functional implementation of fetching config via plugins (ref. #30)
rodzic
fa17911ca2
commit
deeb57a3e2
|
@ -155,6 +155,8 @@ let getConfigJSON = async (cresponse) => {
|
|||
self.log('service-worker', `config.json response status is not 200: ${cdata.status} ${cdata.statusText})`)
|
||||
return false;
|
||||
}
|
||||
// cloning the response before applying json()
|
||||
// so that we can cache the response later
|
||||
var cdata = await cresponse.clone().json()
|
||||
if (verifyConfigData(cdata)) {
|
||||
return cdata;
|
||||
|
@ -177,6 +179,9 @@ let initServiceWorker = async () => {
|
|||
// so that we get an informative message if there's an error
|
||||
try {
|
||||
|
||||
// we'll need this later
|
||||
var cresponse = null
|
||||
|
||||
// get the config
|
||||
//
|
||||
// self.registration.scope contains the scope this service worker is registered for
|
||||
|
@ -187,26 +192,24 @@ let initServiceWorker = async () => {
|
|||
var configURL = self.registration.scope + "config.json"
|
||||
|
||||
// get the config data from cache
|
||||
var cresponse = await caches.match(configURL)
|
||||
cresponse = await caches.match(configURL)
|
||||
var cdata = await getConfigJSON(cresponse)
|
||||
|
||||
// did it work?
|
||||
if (cdata != false) {
|
||||
// we need to know if the config was already cached
|
||||
self.log('service-worker', `valid config file retrieved from cache.`)
|
||||
var wasCached = true
|
||||
|
||||
// cache failed to deliver
|
||||
} else {
|
||||
|
||||
// try fetching directly from the main domain
|
||||
self.log('service-worker', `config file not found in cache, fetching.`)
|
||||
var cresponse = await fetch(configURL)
|
||||
cdata = await getConfigJSON(cresponse)
|
||||
|
||||
// did that work?
|
||||
if (cdata != false) {
|
||||
self.log('service-worker', `config file not found in cache, fetching.`)
|
||||
var wasCached = false
|
||||
// cache it, asynchronously
|
||||
cacheConfigJSON(configURL, cresponse)
|
||||
|
||||
|
@ -320,8 +323,29 @@ let initServiceWorker = async () => {
|
|||
}
|
||||
|
||||
// inform
|
||||
self.log('service-worker', `DEBUG: Strategy in use: ${self.LibResilientPlugins.map(p=>p.name).join(', ')}`)
|
||||
self.log('service-worker', `service worker initialized.\nstrategy in use: ${self.LibResilientPlugins.map(p=>p.name).join(', ')}`)
|
||||
initDone = true;
|
||||
|
||||
// regardless how we got the config file, if it's older than 24h...
|
||||
if ( (new Date()) - Date.parse(cresponse.headers.get('date')) > 86400000) {
|
||||
|
||||
// try to get it asynchronously through the plugins, and cache it
|
||||
self.log('service-worker', `config.json stale, fetching through plugins`)
|
||||
|
||||
getResourceThroughLibResilient(configURL, {}, 'libresilient-internal', false, false)
|
||||
.then(async cresponse=>{
|
||||
|
||||
// extract JSON and verify it
|
||||
var cdata = await getConfigJSON(cresponse)
|
||||
|
||||
// did that work?
|
||||
if (cdata != false) {
|
||||
self.log('service-worker', `config.json successfully fetched through plugins; caching.`)
|
||||
// cache it, asynchronously
|
||||
cacheConfigJSON(configURL, cresponse)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
} catch(e) {
|
||||
// we only get a cryptic "Error while registering a service worker"
|
||||
|
|
Ładowanie…
Reference in New Issue