Commit f5626fdd authored by Romain Courteaud's avatar Romain Courteaud

erp5_web_renderjs_ui: embed the dependency list in the worker code

Global are deleted when the worker is stopped, which make the current fetch code less interesting...
parent 37640816
/*jslint indent: 2*/ /*jslint indent: 2*/
/*global self, caches, fetch, Promise, URL, location*/ /*global self, caches, fetch, Promise, URL, location, JSON*/
(function (self, caches, fetch, Promise, URL, location) { (function (self, caches, fetch, Promise, URL, location, JSON) {
"use strict"; "use strict";
var prefix = location.toString() + '_', var prefix = location.toString() + '_',
CACHE_NAME = prefix + '${modification_date}', CACHE_NAME = prefix + '${modification_date}',
required_url_list = []; REQUIRED_FILES = JSON.parse('${required_url_list}'),
required_url_list = [],
i,
len = REQUIRED_FILES.length;
for (i = 0; i < len; i += 1) {
required_url_list.push(
new URL(REQUIRED_FILES[i], location.toString()).toString()
);
}
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(
fetch('WebSection_getPrecacheManifest') caches.open(CACHE_NAME)
.then(function (response) { .then(function (cache) {
return Promise.all([ var promise = Promise.resolve();
response.json(),
caches.open(CACHE_NAME)
]);
})
.then(function (result_list) {
var required_file_dict = result_list[0],
cache = result_list[1],
key,
promise = Promise.resolve(),
url;
function append(url) { function append(url_to_cache) {
promise = promise promise = promise
.then(function () { .then(function () {
// Use cache.add because safari does not support cache.addAll. // Use cache.add because safari does not support cache.addAll.
return cache.add(url); return cache.add(url_to_cache);
}); });
} }
for (key in required_file_dict) { len = required_url_list.length;
if (required_file_dict.hasOwnProperty(key)) { for (i = 0; i < len; i += 1) {
url = new URL(key, location.toString()).toString(); // Add all offline dependencies to the cache
// Add all offline dependencies to the cache // One by one, to not hammer zopes
// One by one, to not hammer zopes append(required_url_list[i]);
required_url_list.push(url);
append(url);
}
} }
return promise; return promise;
}) })
...@@ -128,4 +124,4 @@ ...@@ -128,4 +124,4 @@
); );
}); });
}(self, caches, fetch, Promise, URL, location)); }(self, caches, fetch, Promise, URL, location, JSON));
\ No newline at end of file \ No newline at end of file
...@@ -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.49192.3036.63283</string> </value> <value> <string>981.49307.35014.63180</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>1581341953.03</float> <float>1581348269.23</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
from DateTime import DateTime from DateTime import DateTime
import json
if REQUEST is None: if REQUEST is None:
REQUEST = context.REQUEST REQUEST = context.REQUEST
...@@ -28,9 +29,12 @@ web_content = web_page.getTextContent() ...@@ -28,9 +29,12 @@ web_content = web_page.getTextContent()
# set headers depending on type of script # set headers depending on type of script
if (portal_type == "Web Script"): if (portal_type == "Web Script"):
response.setHeader('Content-Type', 'application/javascript; charset=utf-8') response.setHeader('Content-Type', 'application/javascript; charset=utf-8')
web_content = web_page.TextDocument_substituteTextContent(web_content, mapping_dict={ if web_page.getTextContentSubstitutionMappingMethodId():
'modification_date': modification_date_string web_content = web_page.TextDocument_substituteTextContent(web_content, mapping_dict={
}) 'modification_date': modification_date_string,
# Make JSLint happy for the service worker code
'required_url_list': json.dumps(web_section.WebSection_getPrecacheManifest())
})
elif (portal_type == "Web Style"): elif (portal_type == "Web Style"):
response.setHeader('Content-Type', 'text/css; charset=utf-8') response.setHeader('Content-Type', 'text/css; charset=utf-8')
......
import json
if REQUEST is None:
REQUEST = context.REQUEST
if response is None:
response = REQUEST.RESPONSE
web_section = context web_section = context
# Add all ERP5JS gadget # Add all ERP5JS gadget
...@@ -180,6 +173,4 @@ precache_manifest_url_list = web_section.getLayoutProperty("configuration_precac ...@@ -180,6 +173,4 @@ precache_manifest_url_list = web_section.getLayoutProperty("configuration_precac
for precache_manifest_script_id in precache_manifest_url_list: for precache_manifest_script_id in precache_manifest_url_list:
url_list.extend(web_section.restrictedTraverse(precache_manifest_script_id)()) url_list.extend(web_section.restrictedTraverse(precache_manifest_script_id)())
result = json.dumps(dict.fromkeys(url_list, None), indent=2) return url_list
response.setHeader('Content-Type', 'application/json')
return result
...@@ -54,7 +54,7 @@ ...@@ -54,7 +54,7 @@
</item> </item>
<item> <item>
<key> <string>_params</string> </key> <key> <string>_params</string> </key>
<value> <string>REQUEST=None, response=None</string> </value> <value> <string></string> </value>
</item> </item>
<item> <item>
<key> <string>_proxy_roles</string> </key> <key> <string>_proxy_roles</string> </key>
......
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