kopia lustrzana https://gitlab.com/rysiekpl/libresilient
service-worker: cleaner handling of config.js (ref. #30)
rodzic
4b02ddc0eb
commit
0a320d22f8
|
@ -140,6 +140,29 @@ let cacheConfigJSON = async (configURL, cresponse) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get config JSON and verify it's valid
|
||||||
|
*
|
||||||
|
* cresponse - the Response object to work with
|
||||||
|
*/
|
||||||
|
let getConfigJSON = async (cresponse) => {
|
||||||
|
if (cresponse == undefined) {
|
||||||
|
self.log('service-worker', 'config.json response is undefined')
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (cresponse.status != 200) {
|
||||||
|
self.log('service-worker', `config.json response status is not 200: ${cdata.status} ${cdata.statusText})`)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
var cdata = await cresponse.clone().json()
|
||||||
|
if (verifyConfigData(cdata)) {
|
||||||
|
return cdata;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// flag signifying the SW has been initialized already
|
// flag signifying the SW has been initialized already
|
||||||
var initDone = false
|
var initDone = false
|
||||||
|
|
||||||
|
@ -159,40 +182,45 @@ let initServiceWorker = async () => {
|
||||||
// self.registration.scope contains the scope this service worker is registered for
|
// self.registration.scope contains the scope this service worker is registered for
|
||||||
// so it makes sense to pull config from `config.json` file directly under that location
|
// so it makes sense to pull config from `config.json` file directly under that location
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// config.json URL
|
// config.json URL
|
||||||
var configURL = self.registration.scope + "config.json"
|
var configURL = self.registration.scope + "config.json"
|
||||||
// we need to know if the config was already cached
|
|
||||||
var wasCached = true
|
// get the config data from cache
|
||||||
// get the config file, cached or otherwise
|
|
||||||
var cresponse = await caches.match(configURL)
|
var cresponse = await caches.match(configURL)
|
||||||
if (cresponse != undefined) {
|
var cdata = await getConfigJSON(cresponse)
|
||||||
self.log('service-worker', `config file retrieved from cache.`)
|
|
||||||
|
// 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 {
|
} else {
|
||||||
self.log('service-worker', `config file not found in cache, fetching.`)
|
|
||||||
wasCached = false
|
// try fetching directly from the main domain
|
||||||
cresponse = await fetch(configURL)
|
var cresponse = await fetch(configURL)
|
||||||
}
|
cdata = await getConfigJSON(cresponse)
|
||||||
// check for sanity
|
|
||||||
// TODO: check also after cache retrieval?
|
// did that work?
|
||||||
if (cresponse.status != 200) {
|
if (cdata != false) {
|
||||||
self.log('service-worker', `failed to fetch config (${cdata.status} ${cdata.statusText}).`)
|
self.log('service-worker', `config file not found in cache, fetching.`)
|
||||||
} else {
|
var wasCached = false
|
||||||
// process the data
|
// cache it, asynchronously
|
||||||
cdata = await cresponse.clone().json()
|
cacheConfigJSON(configURL, cresponse)
|
||||||
// verify the config
|
|
||||||
if (verifyConfigData(cdata)) {
|
|
||||||
// merge the config
|
|
||||||
self.LibResilientConfig = {...self.LibResilientConfig, ...cdata}
|
|
||||||
self.log('service-worker', 'config loaded.')
|
|
||||||
// cache the valid config.json
|
|
||||||
if (!wasCached) {
|
|
||||||
// was not retrieved from cache? cache, then!
|
|
||||||
cacheConfigJSON(configURL, cresponse)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
|
// we ain't got nothing useful -- justs set cdata to an empty object
|
||||||
|
cdata = {}
|
||||||
self.log('service-worker', 'ignoring invalid config, using defaults.')
|
self.log('service-worker', 'ignoring invalid config, using defaults.')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// merge configs
|
||||||
|
self.LibResilientConfig = {...self.LibResilientConfig, ...cdata}
|
||||||
|
self.log('service-worker', 'config loaded.')
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
self.log('service-worker', 'config loading failed, using defaults; error:', e)
|
self.log('service-worker', 'config loading failed, using defaults; error:', e)
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue