Move main and extra scripts to top of page and use defer
#90719
Labels
A-rustdoc-js
Area: Rustdoc's JS front-end
T-rustdoc
Relevant to the rustdoc team, which will review and decide on the PR/issue.
In rustdoc, we currently have a few, small scripts in the
<head>
element, which we allow to be render-blocking because they affect styling:We also have some scripts that show up at the bottom of the page, including in particular the main JS:
Per this Stack Overflow answer, putting scripts at the bottom of the page was a common technique to prevent large scripts from blocking the page render while they download. Now, we can achieve the same result using the
async
ordefer
attributes on the script tag. That has the advantage that it allows the browser to start downloading the JS while it's still downloading and parsing the HTML.defer
is more straightforward because it preserves script ordering and only runs the script once the DOM is ready.async
sometimes requires a little more finagling so the script can handle being run before the DOM is ready (usually you put the DOM-touching parts of the script in an event listener forDOMContentLoaded
.The text was updated successfully, but these errors were encountered: