Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preload styles before deferred scripts #1813

Open
Dani21 opened this issue Jul 8, 2023 · 0 comments
Open

Preload styles before deferred scripts #1813

Dani21 opened this issue Jul 8, 2023 · 0 comments

Comments

@Dani21
Copy link

Dani21 commented Jul 8, 2023

Is your feature request related to a problem? Please describe.
Currently HtmlWebpackPlugin inserts script tags before style tags by default. Deferring script execution and blocking styles is reasonable to load the page quickest without any jumping, and placing script tags before style tags means related assets can download asynchronously. However this also causes the browser to initiate script downloads before styles, and often means if a browser only has one connection open, there's an additional delay to loading styles to initiate the connection (especially if an SSL handshake needs to be done). As such, this can cause scripts to load before styles, even if the stylesheet is smaller.

Describe the solution you'd like
Using <link rel="preload" href="..." as="..."> tags in advance allows the HTML page to direct the order that assets are downloaded. Luckily with HtmlWebpackPlugin's templating parameters, this is easy to do if someone knows how to. However I don't believe there are any examples that demonstrate this in the repo. I would prefer it as an option, but just documenting this would be helpful for others.

Anyways, here's the workaround I've built to do this manually (insert these into the head tag of the template):

<%= htmlWebpackPlugin.files.css.map(url => `<link rel="preload" href="${url}" as="style">`).join('') %>
<%= htmlWebpackPlugin.files.js.map(url => `<link rel="preload" href="${url}" as="script">`).join('') %>

Describe alternatives you've considered
I've tried moving asset tags around, but since it is ideal that styles block page render while scripts are downloaded concurrently (just after styles begin downloading), I believe this is the most optimal solution.

Additional context
Issue #532 alluded to this but didn't show that it improves page downloads nor a concrete example of the impact. In addition, here's a timing graph showing that the second asset encounters a new connection including SSL handshake upon initial load, and that my workaround reduces the download time of the initial CSS:

Timing.Graph.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant