I am working on updating a Google Sign-In component. The Google doc says to include the following in my html file:
<script src="https://accounts.google.com/gsi/client" async defer></script>
Here is the doc Google Doc.
My understanding of the async
and defer
attributes is that they load the script in a different order, and therefor can not both be applied at once. Here is a SO article for reference SO article.
So am I misunderstanding something or why is the Google Document telling me to use both tags?
4
Answers
Yes, they’re mutually exclusive.
async
is related to downloading of the script. It means that the HTML parsing should not be blocked while downloading the script.defer
is related to execution of the script. It means that the execution should be deferred till all the HTML parsing is completed.But as this answer noted, if you specify both,
defer
is ignored.You can still specify both though to support old browsers that don’t support
async
.Yes they are mutually exclusive. When the
async
attribute is present (and supported), thedefer
one is discarded.If you look at the specs, (step 33, temp link).
From this we can see that when the
async
attribute is set, the time of the script’s execution is already defined, and that thedefer
attribute is discarded through the Otherwise logic clause.You can still let both to fallback on the
defer
behavior whenasync
isn’t supported though.defer is ignored when async specified, and async always have higher priority.
but old browsers did not have support for async and it’s rather a new option. so when you write
it means browser should use async method to load script unless it does not support it, if so it can use defer instead.
If you use both async and defer, then defer will take precedence and the script run when the page load is complete, meaning they are not mutually exclusive.