summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js
diff options
context:
space:
mode:
authorqsm-odoo <qsm@odoo.com>2019-03-29 11:13:05 +0000
committerqsm-odoo <qsm@odoo.com>2019-06-04 07:57:20 +0000
commit02dab5dc88112fd5f5ee43882ca022529acb4abf (patch)
tree050f2622037dd35e068ffaa43d596a4665faefc6 /addons/web/static/src/js
parent4f750e705686617d48f73e93b87abeefb7fc87e2 (diff)
[IMP] website, *: lazy load most frontend javascript
* web, survey This commit takes advantage of the other commits of this PR to enable lazy loading for most frontend javascript: 1) Extract the minimum part of the assets common: - promises library and polyfill - boot.js 2) Create "assets_common_lazy" which is "assets_common" - "minimum" 3) Include the "minimum" assets in frontend layouts using "defer" loading and "assets_common_lazy" using "lazy" loading 4) Create a script to do the actual lazy loading in a "minimum" frontend assets 5) Lazy load the current frontend / website editor assets Part of https://github.com/odoo/odoo/pull/32181 task-1961045
Diffstat (limited to 'addons/web/static/src/js')
-rw-r--r--addons/web/static/src/js/public/lazyloader.js42
1 files changed, 42 insertions, 0 deletions
diff --git a/addons/web/static/src/js/public/lazyloader.js b/addons/web/static/src/js/public/lazyloader.js
new file mode 100644
index 00000000000..7c873e1938d
--- /dev/null
+++ b/addons/web/static/src/js/public/lazyloader.js
@@ -0,0 +1,42 @@
+odoo.define('web.public.lazyloader', function (require) {
+'use strict';
+
+var doResolve = null;
+var _allScriptsLoaded = new Promise(function (resolve) {
+ if (doResolve) {
+ resolve();
+ } else {
+ doResolve = resolve;
+ }
+});
+
+window.addEventListener('load', function () {
+ setTimeout(_loadScripts, 0);
+});
+
+function _loadScripts(scripts, index) {
+ if (scripts === undefined) {
+ scripts = document.querySelectorAll('script[data-src]');
+ }
+ if (index === undefined) {
+ index = 0;
+ }
+ if (index >= scripts.length) {
+ if (typeof doResolve === 'function') {
+ doResolve();
+ } else {
+ doResolve = true;
+ }
+ return;
+ }
+ var script = scripts[index];
+ script.addEventListener('load', _loadScripts.bind(this, scripts, index + 1));
+ script.src = script.dataset.src;
+ script.removeAttribute('data-src');
+}
+
+return {
+ loadScripts: _loadScripts,
+ allScriptsLoaded: _allScriptsLoaded,
+};
+});