Commit 6d1a80a5 authored by Romain Courteaud's avatar Romain Courteaud

erp5_web_renderjs_ui: simplify worker

parent 53d8a5df
...@@ -10,51 +10,32 @@ ...@@ -10,51 +10,32 @@
self.addEventListener('install', function (event) { self.addEventListener('install', function (event) {
// Perform install step: loading each required file into cache // Perform install step: loading each required file into cache
event.waitUntil( event.waitUntil(
// We create cache only if it does not exist. That is because fetch('WebSection_getPrecacheManifest')
// we do not want to override an existing cache by mistake. .then(function (response) {
// Code consistency is very important. We must not mix different return Promise.all([
// versions of code. response.json(),
// (For example, developer change service worker code and forget caches.open(CACHE_NAME)
// to increase the cache version.) ]);
caches.has(CACHE_NAME) })
.then(function (result) { .then(function (result_list) {
if (!result) { var required_file_dict = result_list[0],
return fetch('WebSection_getPrecacheManifest') cache = result_list[1],
.then(function (response) { key,
return Promise.all([ promise_list = [],
response.json(), url;
caches.open(CACHE_NAME)
]);
})
.then(function (result_list) {
var required_file_dict = result_list[0],
cache = result_list[1],
key,
promise_list = [],
url;
for (key in required_file_dict) {
if (required_file_dict.hasOwnProperty(key)) {
url = new URL(key, location.toString()).toString();
required_url_list.push(url);
// Use cache.add because safari does not support cache.addAll.
// console.log("Install " + CACHE_NAME + " = " + url);
promise_list.push(cache.add(url));
}
}
// Add all offline dependencies to the cache for (key in required_file_dict) {
return Promise.all(promise_list); if (required_file_dict.hasOwnProperty(key)) {
}) url = new URL(key, location.toString()).toString();
.catch(function (error) { required_url_list.push(url);
// Since we do not allow to override existing cache, if cache installation // Use cache.add because safari does not support cache.addAll.
// failed, we need to delete the cache completely. // console.log("Install " + CACHE_NAME + " = " + url);
caches.delete(CACHE_NAME); promise_list.push(cache.add(url));
// Explicitly unregister service worker else it may not be done. }
self.registration.unregister();
throw error;
});
} }
// Add all offline dependencies to the cache
return Promise.all(promise_list);
}) })
.then(function () { .then(function () {
// When user accesses ERP5JS web site first time, service worker is // When user accesses ERP5JS web site first time, service worker is
...@@ -67,6 +48,14 @@ ...@@ -67,6 +48,14 @@
// the first one. // the first one.
return self.skipWaiting(); return self.skipWaiting();
}) })
.catch(function (error) {
// Since we do not allow to override existing cache, if cache installation
// failed, we need to delete the cache completely.
caches.delete(CACHE_NAME);
// Explicitly unregister service worker else it may not be done.
self.registration.unregister();
throw error;
})
); );
}); });
......
...@@ -238,7 +238,7 @@ ...@@ -238,7 +238,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>981.45037.17470.33382</string> </value> <value> <string>981.48930.15634.60876</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -256,7 +256,7 @@ ...@@ -256,7 +256,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1581092014.2</float> <float>1581325589.75</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
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