[FIX] web, base: review lazy loaded <script> definitions

The assets bundle system allows to generate `<script>` elements whose
resource is meant to be lazy loaded. In that case, a "data-src" is used
instead of "src" directly as attribute. This is valid HTML code: "src"
is not required. However... this lazy loading system also automatically
added `defer="defer"` which makes the "src" attribute required, thus
failing W3C validation.

Now, we only add "data-src" and the `defer="defer"` part is added on the
client-side, once the "data-src" is switched to "src". Note that this
"defer" attribute may not be needed in this case at all, but it cannot
hurt.

Part-of: odoo/odoo#120311
This commit is contained in:
qsm-odoo
2023-05-11 18:25:08 +02:00
parent ad63194af3
commit 1b428bd0b0
3 changed files with 7 additions and 2 deletions
@@ -103,6 +103,7 @@ function _loadScripts(scripts, index) {
}
var script = scripts[index];
script.addEventListener('load', _loadScripts.bind(this, scripts, index + 1));
script.setAttribute('defer', 'defer');
script.src = script.dataset.src;
script.removeAttribute('data-src');
}
+3 -1
View File
@@ -191,7 +191,9 @@ class AssetsBundle(object):
src = self.get_debug_asset_url(name=js_attachment.name, extension='') if is_debug_assets else js_attachment[0].url
attr = dict([
["async", "async" if async_load else None],
["defer", "defer" if defer_load or lazy_load else None],
# lazy_load will add defer in JS otherwise this is not W3C valid
# (defer is probably not needed there anyways)
["defer", "defer" if defer_load else None],
["type", "text/javascript"],
["data-src" if lazy_load else "src", src],
['data-asset-bundle', self.name],
+3 -1
View File
@@ -2512,7 +2512,9 @@ class IrQWeb(models.AbstractModel):
"type": mimetype,
}
attributes["data-src" if lazy_load else "src"] = path
if defer_load or lazy_load:
if defer_load:
# lazy_load will add defer in JS otherwise this is not
# W3C valid (defer is probably not needed there anyways)
attributes["defer"] = "defer"
elif is_css:
tag = 'link'