Skip to content

Commit 9f480a4

Browse files
committed
chore: replace doc-ready with defer
Replace using `async` with `defer` to allow parallel download of JavaScript without blocking the parser. Now script will run after DOM is parsed. Refs: https://html.spec.whatwg.org/multipage/scripting.html#attr-script-defer Refs: https://caniuse.com/script-defer Signed-off-by: Mike Fiedler <miketheman@gmail.com>
1 parent f578120 commit 9f480a4

File tree

6 files changed

+40
-66
lines changed

6 files changed

+40
-66
lines changed

warehouse/static/js/warehouse/index.js

Lines changed: 34 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ import { Application } from "@hotwired/stimulus";
55
import { definitionsFromContext } from "@hotwired/stimulus-webpack-helpers";
66
import { Autocomplete } from "stimulus-autocomplete";
77

8-
// We'll use docReady as a modern replacement for $(document).ready() which
9-
// does not require all of jQuery to use. This will let us use it without
10-
// having to load all of jQuery, which will make things faster.
11-
import docReady from "warehouse/utils/doc-ready";
12-
138
// Import our utility functions
149
import HTMLInclude from "warehouse/utils/html-include";
1510
import * as formUtils from "warehouse/utils/forms";
@@ -23,63 +18,51 @@ import {GuardWebAuthn, AuthenticateWebAuthn, ProvisionWebAuthn} from "warehouse/
2318
import checkProxyProtection from "warehouse/utils/proxy-protection";
2419

2520
// Show unsupported browser warning if necessary
26-
docReady(() => {
27-
if (navigator.appVersion.includes("MSIE 10")) {
28-
if (document.getElementById("unsupported-browser") !== null) return;
29-
21+
if (navigator.appVersion.includes("MSIE 10")) {
22+
if (document.getElementById("unsupported-browser") === null) {
3023
let warning_div = document.createElement("div");
3124
warning_div.innerHTML = "<div id='unsupported-browser' class='notification-bar notification-bar--warning' role='status'><span class='notification-bar__icon'><i class='fa fa-exclamation-triangle' aria-hidden='true'></i><span class='sr-only'>Warning:</span></span><span class='notification-bar__message'>You are using an unsupported browser, please upgrade to a newer version.</span></div>";
3225

3326
document.getElementById("sticky-notifications").appendChild(warning_div);
3427
}
35-
});
28+
}
3629

3730
// Human-readable timestamps for project histories
38-
docReady(() => {
39-
timeAgo();
40-
});
31+
timeAgo();
4132

4233
// toggle search panel behavior
43-
docReady(() => {
44-
if (document.querySelector(".-js-add-filter")) searchFilterToggle();
45-
});
34+
if (document.querySelector(".-js-add-filter")) searchFilterToggle();
4635

4736
// Kick off the client side HTML includes.
48-
docReady(HTMLInclude);
37+
HTMLInclude();
4938

5039
// Handle the JS based automatic form submission.
51-
docReady(formUtils.submitTriggers);
52-
docReady(formUtils.registerFormValidation);
40+
formUtils.submitTriggers();
41+
formUtils.registerFormValidation();
5342

54-
docReady(Statuspage);
43+
Statuspage();
5544

5645
// Close modals when escape button is pressed
57-
docReady(() => {
58-
document.addEventListener("keydown", event => {
59-
// Only handle the escape key press when a modal is open
60-
if (document.querySelector(".modal:target") && event.keyCode === 27) {
61-
for (let element of document.querySelectorAll(".modal")) {
62-
application
63-
.getControllerForElementAndIdentifier(element, "confirm")
64-
.cancel();
65-
}
46+
document.addEventListener("keydown", event => {
47+
// Only handle the escape key press when a modal is open
48+
if (document.querySelector(".modal:target") && event.keyCode === 27) {
49+
for (let element of document.querySelectorAll(".modal")) {
50+
application
51+
.getControllerForElementAndIdentifier(element, "confirm")
52+
.cancel();
6653
}
67-
});
54+
}
6855
});
6956

7057
// Position sticky bar
71-
docReady(() => {
72-
setTimeout(PositionWarning, 200);
73-
});
58+
setTimeout(PositionWarning, 200);
7459

75-
docReady(() => {
76-
let resizeTimer;
77-
const onResize = () => {
78-
clearTimeout(resizeTimer);
79-
resizeTimer = setTimeout(PositionWarning, 200);
80-
};
81-
window.addEventListener("resize", onResize, false);
82-
});
60+
let resizeTimer;
61+
const onResize = () => {
62+
clearTimeout(resizeTimer);
63+
resizeTimer = setTimeout(PositionWarning, 200);
64+
};
65+
window.addEventListener("resize", onResize, false);
8366

8467
let bindDropdowns = function () {
8568
// Bind click handlers to dropdowns for keyboard users
@@ -137,33 +120,29 @@ let bindDropdowns = function () {
137120
};
138121

139122
// Bind the dropdowns when the page is ready
140-
docReady(bindDropdowns);
123+
bindDropdowns();
141124

142125
// Get modal keypress event listeners ready
143-
docReady(BindModalKeys);
126+
BindModalKeys();
144127

145128
// Get filter pane keypress event listeners ready
146-
docReady(BindFilterKeys);
129+
BindFilterKeys();
147130

148131
// Get WebAuthn compatibility checks ready
149-
docReady(GuardWebAuthn);
132+
GuardWebAuthn();
150133

151134
// Get WebAuthn provisioning ready
152-
docReady(ProvisionWebAuthn);
135+
ProvisionWebAuthn();
153136

154137
// Get WebAuthn authentication ready
155-
docReady(AuthenticateWebAuthn);
138+
AuthenticateWebAuthn();
156139

157140
// Initialize proxy protection
158-
docReady(checkProxyProtection);
141+
checkProxyProtection();
159142

160-
docReady(() => {
161-
const tokenSelect = document.getElementById("token_scope");
162-
163-
if (tokenSelect === null) {
164-
return;
165-
}
143+
const tokenSelect = document.getElementById("token_scope");
166144

145+
if (tokenSelect !== null) {
167146
tokenSelect.addEventListener("change", () => {
168147
const tokenScopeWarning = document.getElementById("api-token-scope-warning");
169148
if (tokenScopeWarning === null) {
@@ -173,7 +152,7 @@ docReady(() => {
173152
const tokenScope = tokenSelect.options[tokenSelect.selectedIndex].value;
174153
tokenScopeWarning.hidden = (tokenScope !== "scope:user");
175154
});
176-
});
155+
}
177156

178157
// Bind again when client-side includes have been loaded (for the logged-in
179158
// user dropdown)

warehouse/static/js/warehouse/utils/doc-ready.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

warehouse/templates/accounts/register.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,6 @@ <h1 class="page-title">{% trans title=title %}Create an account on {{ title }}{%
220220
{% endblock %}
221221
{% block extra_js %}
222222
{{ captcha_src(request) }}
223-
<script async
223+
<script defer
224224
src="{{ request.static_path('warehouse:static/dist/js/vendor/zxcvbn.js') }}"></script>
225225
{% endblock %}

warehouse/templates/accounts/reset-password.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,6 @@ <h1 class="page-title">{% trans %}Reset your password{% endtrans %}</h1>
9595
</div>
9696
{% endblock %}
9797
{% block extra_js %}
98-
<script async
98+
<script defer
9999
src="{{ request.static_path('warehouse:static/dist/js/vendor/zxcvbn.js') }}"></script>
100100
{% endblock %}

warehouse/templates/base.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,12 @@
164164
type="application/opensearchdescription+xml"
165165
title="PyPI"
166166
href="{{ request.route_path('opensearch.xml') }}">
167-
{# All of our JavaScript should be loaded using the async attribute so it's
167+
{# All of our JavaScript should be loaded using either the `async` or `defer` attribute so it's
168168
# fine to have it in the header. This will allow the browser to start
169169
# downloading it ASAP while still not blocking the parser.
170+
# Use `async` if the script can be executed before the DOM is fully parsed.
170171
#}
171-
<script async src="{{ request.static_path('warehouse:static/dist/js/warehouse' + ('' if request.localizer.locale_name == 'en' else '.' + request.localizer.locale_name) + '.js') }}">
172+
<script defer src="{{ request.static_path('warehouse:static/dist/js/warehouse' + ('' if request.localizer.locale_name == 'en' else '.' + request.localizer.locale_name) + '.js') }}">
172173
</script>
173174
{% block extra_js %}{% endblock %}
174175
{# Exclude all query parameters from analytics payload on pages where they may contain sensitive information.

warehouse/templates/manage/account.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,6 @@ <h3>{% trans %}Proceed with caution!{% endtrans %}</h3>
851851
</section>
852852
{% endblock %}
853853
{% block extra_js %}
854-
<script async
854+
<script defer
855855
src="{{ request.static_path('warehouse:static/dist/js/vendor/zxcvbn.js') }}"></script>
856856
{% endblock %}

0 commit comments

Comments
 (0)