Commit fe1fceef authored by Yusei Tahara's avatar Yusei Tahara Committed by Romain Courteaud

[erp5_web_renderjs_ui] Fix service worker. Don't hardcode the special cache name.

The name must be different per web site, else if the same service worker code is used by multiple web sites in ERP5 web site module, service worker does not work correctly.
parent e15d490b
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
"use strict"; "use strict";
var prefix = location.toString() + '_', var prefix = location.toString() + '_',
// CLIENT_CACHE_MAPPING_NAME must not start with `prefix`
// else it may be used as a normal content cache.
CLIENT_CACHE_MAPPING_NAME = '__erp5js_' + location.toString(),
CACHE_NAME = prefix + '_0001', CACHE_NAME = prefix + '_0001',
CACHE_MAP = {}, CACHE_MAP = {},
// Files required to make this app work offline // Files required to make this app work offline
...@@ -213,7 +216,7 @@ ...@@ -213,7 +216,7 @@
key, it must not use different Caches. Since service worker key, it must not use different Caches. Since service worker
is stateless, to maintain the mapping of client and Cache key, is stateless, to maintain the mapping of client and Cache key,
we use Cache Storage as a persistent data store. The key of we use Cache Storage as a persistent data store. The key of
this special Cache is `__erp5js`. this special Cache is CLIENT_CACHE_MAPPING_NAME.
*/ */
var url = new URL(event.request.url), var url = new URL(event.request.url),
client_id = event.clientId.toString(), client_id = event.clientId.toString(),
...@@ -247,15 +250,15 @@ ...@@ -247,15 +250,15 @@
Promise.resolve() Promise.resolve()
.then(function () { .then(function () {
if (!CACHE_KEY) { if (!CACHE_KEY) {
// __erp5js stores CACHE_KEY of each client. // CLIENT_CACHE_MAPPING_NAME stores CACHE_KEY of each client.
return caches.open('__erp5js'); return caches.open(CLIENT_CACHE_MAPPING_NAME);
} }
}) })
.then(function (erp5js_cache) { .then(function (erp5js_cache) {
if (erp5js_cache) { if (erp5js_cache) {
// Service worker forget everything when it stops. So, when it started // Service worker forget everything when it stops. So, when it started
// again, CACHE_MAP is empty, get the associated CACHE_KEY from the // again, CACHE_MAP is empty, get the associated CACHE_KEY from the
// special Cache named __erp5js. // special Cache named CLIENT_CACHE_MAPPING_NAME.
ERP5JS_CACHE = erp5js_cache; ERP5JS_CACHE = erp5js_cache;
return erp5js_cache.match(client_id); return erp5js_cache.match(client_id);
} }
...@@ -345,7 +348,7 @@ ...@@ -345,7 +348,7 @@
); );
}) })
.then(function () { .then(function () {
caches.delete("__erp5js"); 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.14183.65427.5973</string> </value> <value> <string>976.15165.53290.29832</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>1559839835.03</float> <float>1559898683.77</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