Search code examples
odoo-15

odoo 15: W3C validator: Element script must not have attribute defer unless attribute src is also specified


I'm working with odoo 15, when I use the w3c validator on my home page code I get this error: Element script must not have attribute defer unless attribute src is also specified.

<script defer="defer" type="text/javascript" data-src="/web/assets/16708-d6c51d3/1/web.assets_frontend_lazy.min.js" data-asset-bundle="web.assets_frontend_lazy" data-asset-version="d6c51d3"></script>

how to fix the error to comply with HTML specifications. I inspected odoo addons and found this file

odoo15/odoo/addons/base/models/assetsbundle.py:
def to_node(self, css=True, js=True, debug=False, async_load=False, defer_load=False, lazy_load=False):
        """
:returns [(tagName, attributes, content)] if the tag is auto close
"""
response = []
is_debug_assets = debug and 'assets' in debug
if css and self.stylesheets:
    css_attachments = self.css(is_minified=not is_debug_assets) or []
    for attachment in css_attachments:
        if is_debug_assets:
            href = self.get_debug_asset_url(extra='rtl/' if self.user_direction == 'rtl' else '',
                                            name=css_attachments.name,
                                            extension='')
        else:
            href = attachment.url
        attr = dict([
            ["type", "text/css"],
            ["rel", "stylesheet"],
            ["href", href],
            ['data-asset-bundle', self.name],
            ['data-asset-version', self.version],
        ])
        response.append(("link", attr, None))
    if self.css_errors:
        msg = '\n'.join(self.css_errors)
        response.append(JavascriptAsset(self, inline=self.dialog_message(msg)).to_node())
        response.append(StylesheetAsset(self, url="/web/static/lib/bootstrap/dist/css/bootstrap.css").to_node())

if js and self.javascripts:
    js_attachment = self.js(is_minified=not is_debug_assets)
    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],
        ["type", "text/javascript"],
        ["data-src" if lazy_load else "src", src],
        ['data-asset-bundle', self.name],
        ['data-asset-version', self.version],
    ])
    response.append(("script", attr, None))

return response

as you see there is this line ["data-src" if lazy_load else "src", src], is it safe to inherit the class AssetsBundle and change it by ["src", src] to fix the error ?

I tried ["src", src] but not sure if it was safe or not,like maybe it caused some internal problems that I'm not aware of


Solution

  • It wouldn't make any difference since what you will be having it's a direct resource download when you change data-src for src because data-src will be used to lazy load the resource after everything else it's already downloaded or when it's needed.

    It will maybe make your page load a little slow but perhaps you didn't even notice that