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

Prefer <script defer> over <script async> #1916

Closed
wants to merge 3 commits into from
Closed

Prefer <script defer> over <script async> #1916

wants to merge 3 commits into from

Conversation

Rowno
Copy link
Contributor

@Rowno Rowno commented May 8, 2017

Prefer <script defer> over <script async> so that quickly downloaded scripts don't block the initial render. This also ensures that the page script is always executed before the error script since async scripts can execute in any order.

I also moved app.js to the top of the script lists since it has to be downloaded/executed before anything else can actually initialise (the browser uses the <link rel='preload'> order to infer the download priority).

See: https://calendar.perfplanet.com/2016/prefer-defer-over-async/

@timneutkens timneutkens requested a review from arunoda May 8, 2017 19:54
@arunoda
Copy link
Contributor

arunoda commented May 9, 2017

quickly downloaded scripts don't block the initial render.

You've a valid point on this. But this is not the solution.
BTW: We need error and current page's JS stuff to run the app. So, it'll wait.

We can't use defer since it always blocks the HTML rendering.
(We had use defer previously)

Since we load all the scripts at the end, this won't be an issue.
And the fix for this issue should is check page load event in app.js and run it once the DOMContentLoaded event runs.

I'd like to have a PR for that.
Here's we can do it: https://github.com/zeit/next.js/blob/master/client/index.js#L52

@arunoda arunoda closed this May 9, 2017
@arunoda arunoda removed their request for review May 9, 2017 04:18
@Rowno
Copy link
Contributor Author

Rowno commented May 9, 2017

When you use async the browser will still block the HTML parser to compile and partially execute the scripts as soon as they're downloaded. As the article you linked says, defer won't block HTML parser:

async downloads the file during HTML parsing and will pause the HTML parser to execute it when it has finished downloading.

defer downloads the file during HTML parsing and will only execute it after the parser has completed.

@Rowno
Copy link
Contributor Author

Rowno commented May 9, 2017

Also consider the case where the user already has the JavaScript files cached. async will cause them to execute immediately, blocking the initial render, whereas defer will not.

@arunoda
Copy link
Contributor

arunoda commented May 9, 2017

@Rowno we tested many cases.
Check this PR for more info: #1436

@Rowno
Copy link
Contributor Author

Rowno commented May 9, 2017

Ok, thanks.

@lock lock bot locked as resolved and limited conversation to collaborators May 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants