django-vite: Script tag shouldn’t have defer, possibly nor async

The defer and async are added to the script tag:

<script type="module" async="" defer="" src="/static/app.f0505013.js"></script>

However, the defer attribute is implied by browsers that support type="module", so it shouldn’t be added.

Regarding async, it seems it might block the parsing of the DOM to execute the code as soon as possible, out of order.
IMHO it would be better to remove it, or leave the lib user to add it.

Resource: https://gist.github.com/jakub-g/385ee6b41085303a53ad92c7c8afd7a6

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (13 by maintainers)

Commits related to this issue

Most upvoted comments

Sorry I’m an idiot 😅 I forgot that we are talking about modules … I’ll fix that.

  • There is no need to use the defer attribute (see <script> attributes) when loading a module script; modules are deferred automatically.

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#other_differences_between_modules_and_standard_scripts

Hi, i’m currently working on a version 2.0 of Django Vite, it will make easier to customize async, defer and custom attributes. It will also support pathlib.Path sor settings.

Stay tune.

Hello, for info, with the patch 9402410 and the template tag bellow the script tag can be inserted by specifying any attribute the user needs; e.g.:

{% theme_vite_asset 'stuff/static/js/main.js' type='module' %}

Template tag:

@register.simple_tag
@mark_safe
def theme_vite_asset(path, *args, **kwargs):
    scripts_attrs = kwargs
    return DjangoViteAssetLoader.instance().generate_vite_asset(
        path, scripts_attrs=scripts_attrs
    )

Exactly, great! I’ll work on it on the next days.