Add "defer" or "async" to view.js script tag

Permalink
How would I go about making sure that any view.js script tags that are automatically being included have "defer" or "async" attributes assigned to them?

 
mnakalay replied on at Permalink Reply
mnakalay
You would have to override the file responsible for building each JavaScript asset's element, then check that it is one of the view.js file or inside a block's js folder and for those you'd have to add the attribute you want.

For JavaScript assstes it's concrete\src\Asset\JavascriptAsset.php

But it won't necessarily do you any good. For instance, if you set your cache to minify and concatenate JS and CSS, you won't necessarily be able to separate those files from others.

When C5 concatenates JS files, for instance, it adds a data-source attribute to the script element. That data-source lists all the scripts that were concatenated in this one script.

So you could add defer or async to that one but it will probably be a big mix of different scripts, not necessary only the ones you want.
JohntheFish replied on at Permalink Reply
JohntheFish
If you want to do this for a script you are writing for a custom block, you could explicitly load it with AddheaderItem or AddFooterItem.