react-helmet icon indicating copy to clipboard operation
react-helmet copied to clipboard

Fix: async property of scripts is not assigned properly

Open gp27 opened this issue 4 years ago • 4 comments

This fix addresses a problem that happens when trying to set the async attribute of a script tag. Intuitively, setting the async attribute on the tag should also set the the element async property. However this is not the case.

This not a bug but part of the specification. The async attribute is used only by the HTML/XML parser when the page is initially loaded, but it's ignored on dynamic scripts, which are async by default (you can check the section about "force-async": https://www.w3.org/TR/2014/PR-html5-20140916/scripting-1.html ).

For this reason script.setAttribute('async', value) will not work as expected. In order to actually change the async behavior of the tag, the property must be set directly with script.async = value.

Setting async=false is particularly useful to maintain the execution order of dynamic scripts. Inside issue #419 someone actually suggested this solution, but when I tried some code like the following one the tags executed out of order most of the time.

<Helmet>
  <script async={false} src="big-file.js" />
  <script async={false} src="small-file.js" /> 
</Helmet>

Related issues: #419

gp27 avatar Mar 01 '22 16:03 gp27

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Mar 01 '22 16:03 CLAassistant

is this going to be merged?

davideghz avatar Nov 12 '22 19:11 davideghz

Bump. When will this be merged?

annale84 avatar Jan 13 '23 22:01 annale84

I'm going to leave here the patch file for anyone who might need it: https://pastebin.com/fmHGfSFb

You can use patch-package to apply it in your project.

gp27 avatar Jan 14 '23 09:01 gp27