Commit 731e6160 authored by Yusei Tahara's avatar Yusei Tahara Committed by Romain Courteaud

[erp5_web_renderjs_ui] Add more comments because service worker is unstable and hard to use safely.

parent 4493f9c6
...@@ -168,6 +168,12 @@ ...@@ -168,6 +168,12 @@
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
// we do not want to override an existing cache by mistake.
// Code consistency is very important. We must not mix different
// versions of code.
// (For example, developer change service worker code and forget
// to increase the cache version.)
caches.has(CACHE_NAME) caches.has(CACHE_NAME)
.then(function (result) { .then(function (result) {
if (!result) { if (!result) {
...@@ -192,6 +198,17 @@ ...@@ -192,6 +198,17 @@
.then(function (keys) { .then(function (keys) {
keys = keys.filter(function (key) {return key.startsWith(prefix); }); keys = keys.filter(function (key) {return key.startsWith(prefix); });
if (keys.length === 1) { if (keys.length === 1) {
// When user accesses ERP5JS web site first time, service worker is
// installed but it is not activated yet, service worker is activated
// when the page is refreshed or when a new tab opens the site again.
// If user does not refresh the page and continue to use the site,
// user can't use cache, so everything becomes slow. We must avoid this
// situation.
// So, we want to activate the new service worker immediately if it was
// the first one. (We must not activate the new service worker by
// skipWaiting if there is already an active service worker because it
// causes code inconsistency by loading code from a different version of
// cache.
// If there is only one cache, it means that this is the first service worker, // If there is only one cache, it means that this is the first service worker,
// thus we can do skipWaiting. And self.registration is unreliable on // thus we can do skipWaiting. And self.registration is unreliable on
// Firefox, we can't use self.registration.active // Firefox, we can't use self.registration.active
...@@ -199,6 +216,8 @@ ...@@ -199,6 +216,8 @@
} }
}) })
.catch(function () { .catch(function () {
// Since we do not allow to override existing cache, if cache installation
// failed, we need to delete the cache completely.
caches.delete(CACHE_NAME); caches.delete(CACHE_NAME);
throw "Download failed! Deleted " + CACHE_NAME; throw "Download failed! Deleted " + CACHE_NAME;
}); });
...@@ -348,6 +367,9 @@ ...@@ -348,6 +367,9 @@
); );
}) })
.then(function () { .then(function () {
// If new service worker becomes active, it means that all clients
// (tabs, windows, etc) were already closed. Thus we can remove the
// client cache mapping.
caches.delete(CLIENT_CACHE_MAPPING_NAME); caches.delete(CLIENT_CACHE_MAPPING_NAME);
}) })
); );
......
...@@ -228,7 +228,7 @@ ...@@ -228,7 +228,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>976.15165.53290.29832</string> </value> <value> <string>976.15237.62291.34798</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -246,7 +246,7 @@ ...@@ -246,7 +246,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1559898683.77</float> <float>1559902712.53</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