It was mentioned in docs that it is good practise to put scripts in the head of the page with defer attribute. That gives a very good performance boost since the loading of the main scripts is not blocking the rendering of the HTML on a first load.
We are using the hot-controls and have them inside pages and partials as js[] = “path/to/controls.js”
These scripts are loaded prior framework, causing errors then first loaded. Is there a way to defer load them something like: {% scripts defer %} or {% put scripts defer %} ? Through the compoment imho that was possible.
If there is another approarch please assist with idea.
Best, Max
P.s.: We tried to put all the scripts into one js file and defer load it. Works great, but having few third party libs in there is not an option for pages without these functionality.
Im using {% put %} with and it is working just fine.
Although inline scripts in HTML will not work, since the loading order will be different. Inline scripts will be executed before the core, so avoid using them.
But I’m getting this error in the browser: “Uncaught ReferenceError: oc is not defined” because the code in {% put scripts %} is executed before the JavaScript framework is loaded. Do you have a solution to counter this problem?
You could either move the controls into a separate JS file and append it to the list of scripts in your head, integrate them into your app.js or do the classic “wait until window or DOM are ready”.
There are a few ways to do it, I think this one should work fine for your case:
This would probably not work as well or will not be stable because of the racing conditions. Right after the document is ready the script code from the page will be executed first prior to defer scripts loaded.