django-bootstrap5/packaged javascript defer loading

Hi all, in django-bootstrap5, you’d load the relevant javascript with {% bootstrap_javascript %}, and this generates a script tag loading the bootstrap javscript.

Is there a way to add defer or async to this script tag?

I believe you can “hack” your way in.
In the settings documentation there’s a configuration section, in that section there’s a section for the javascript_url. I digged in the source code and it looks it may work.

BOOSTRAP5 = {
     # The complete URL to the Bootstrap bundle JavaScript file.
    "javascript_url": {
        "url": "https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js",
        "integrity": "sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa",
        "crossorigin": "anonymous",
        # Add here, this should do the trick
        "async": "true"
    },
}```

Try this and let me know if it worked.

Works! Thank you @leandrodesouzadev . For others using this method, make sure to change the bootstrap link and fingerprint to your default value. Also BOOTSTRAP_5 should be BOOTSTRAP5:

BOOTSTRAP5 = {
    'javascript_url': {
        #options
    }
}
1 Like