kopia lustrzana https://gitlab.com/rysiekpl/libresilient
separated out some code into cacheConfigJSON() (prep. for #30)
rodzic
c2b62919b8
commit
7809c9d42e
|
@ -123,6 +123,23 @@ let verifyConfigData = (cdata) => {
|
|||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* cache the `config.json` response, wherever from we got it
|
||||
*
|
||||
* configURL - url of the config file
|
||||
* cresponse - response we're caching
|
||||
*/
|
||||
let cacheConfigJSON = async (configURL, cresponse) => {
|
||||
try {
|
||||
var cache = await caches.open('v1')
|
||||
await cache.put(configURL, cresponse)
|
||||
self.log('service-worker', 'config cached.')
|
||||
} catch(e) {
|
||||
self.log('service-worker', `failed to cache config: ${e}`)
|
||||
}
|
||||
}
|
||||
|
||||
// flag signifying the SW has been initialized already
|
||||
var initDone = false
|
||||
|
||||
|
@ -141,9 +158,6 @@ let initServiceWorker = async () => {
|
|||
//
|
||||
// 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
|
||||
//
|
||||
// TODO: providing config directly from browser-side control script via postMessage?
|
||||
// TODO: `updateViaCache=imports` allows at least config.json to be updated using the cache plugin?
|
||||
try {
|
||||
// config.json URL
|
||||
var configURL = self.registration.scope + "config.json"
|
||||
|
@ -159,6 +173,7 @@ let initServiceWorker = async () => {
|
|||
cresponse = await fetch(configURL)
|
||||
}
|
||||
// check for sanity
|
||||
// TODO: check also after cache retrieval?
|
||||
if (cresponse.status != 200) {
|
||||
self.log('service-worker', `failed to fetch config (${cdata.status} ${cdata.statusText}).`)
|
||||
} else {
|
||||
|
@ -171,13 +186,8 @@ let initServiceWorker = async () => {
|
|||
self.log('service-worker', 'config loaded.')
|
||||
// cache the valid config.json
|
||||
if (!wasCached) {
|
||||
try {
|
||||
var cache = await caches.open('v1')
|
||||
await cache.put(configURL, cresponse)
|
||||
self.log('service-worker', 'config cached.')
|
||||
} catch(e) {
|
||||
self.log('service-worker', `failed to cache config: ${e}`)
|
||||
}
|
||||
// was not retrieved from cache? cache, then!
|
||||
cacheConfigJSON(configURL, cresponse)
|
||||
}
|
||||
} else {
|
||||
self.log('service-worker', 'ignoring invalid config, using defaults.')
|
||||
|
|
Ładowanie…
Reference in New Issue