Skip to content

Commit c7382a9

Browse files
committed
Load JavaScript resources asynchronously
Use the async attribute of the `<script>` element to load the scripts asynchronously. From https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-async > ..., if the async attribute is present, then the classic script will > be fetched in parallel to parsing and evaluated as soon as it is > available. > > ... > > This attribute allows the elimination of parser-blocking JavaScript > where the browser would have to load and evaluate scripts before > continuing to parse. IMO, async is preferable to defer for two reasons, 1. Scripts with the defer attribute will prevent the DOMContentLoaded event from firing until the script has loaded and finished evaluating. The django-debug-toolbar scripts should not block this event. This is now avoided by checking the document state before calling djdt.init(). 2. The defer attribute has no effect on module scripts. Now that JavaScript modules are in the ES6 spec and available in modern supported browsers, django-debug-toolbar can begin moving towards use them which offer many advantages over the classical `<script>` element. Upon moving to modules, the scripts should continue to load asynchronously.
1 parent 70a1ecf commit c7382a9

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

debug_toolbar/static/debug_toolbar/js/toolbar.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
root.querySelectorAll('script').forEach(function(e) {
3030
var clone = document.createElement('script');
3131
clone.src = e.src;
32+
clone.async = true;
3233
root.appendChild(clone);
3334
});
3435
},
@@ -306,5 +307,10 @@
306307
close: djdt.hide_one_level,
307308
cookie: djdt.cookie,
308309
};
309-
document.addEventListener('DOMContentLoaded', djdt.init);
310+
311+
if (document.readyState !== 'loading') {
312+
djdt.init();
313+
} else {
314+
document.addEventListener('DOMContentLoaded', djdt.init);
315+
}
310316
})();

debug_toolbar/templates/debug_toolbar/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{% load i18n %}{% load static %}
22
<link rel="stylesheet" href="{% static 'debug_toolbar/css/print.css' %}" media="print">
33
<link rel="stylesheet" href="{% static 'debug_toolbar/css/toolbar.css' %}">
4-
<script src="{% static 'debug_toolbar/js/toolbar.js' %}" defer></script>
4+
<script src="{% static 'debug_toolbar/js/toolbar.js' %}" async></script>
55
<div id="djDebug" class="djdt-hidden" dir="ltr"
66
{% if toolbar.store_id %}data-store-id="{{ toolbar.store_id }}" data-render-panel-url="{% url 'djdt:render_panel' %}"{% endif %}
77
{{ toolbar.config.ROOT_TAG_EXTRA_ATTRS|safe }}>

debug_toolbar/templates/debug_toolbar/panels/timer.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ <h4>{% trans "Browser timing" %}</h4>
4141
</tbody>
4242
</table>
4343
</div>
44-
<script src="{% static 'debug_toolbar/js/toolbar.timer.js' %}" defer></script>
44+
<script src="{% static 'debug_toolbar/js/toolbar.timer.js' %}" aysnc></script>

debug_toolbar/templates/debug_toolbar/redirect.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
<!DOCTYPE html>
33
<html>
44
<head>
5+
<script src="{% static 'debug_toolbar/js/redirect.js' %}" async></script>
56
</head>
67
<body>
78
<h1>{{ status_line }}</h1>
89
<h2>{% trans "Location:" %} <a id="redirect_to" href="{{ redirect_to }}">{{ redirect_to }}</a></h2>
910
<p class="notice">
1011
{% trans "The Django Debug Toolbar has intercepted a redirect to the above URL for debug viewing purposes. You can click the above link to continue with the redirect as normal." %}
1112
</p>
12-
<script src="{% static 'debug_toolbar/js/redirect.js' %}" defer></script>
1313
</body>
1414
</html>

0 commit comments

Comments
 (0)