I don’t have a testing environment to validate this technique, but it exists and is even described in that article by Google itself. But as I’m saying it’s up to you to test for the result...
What we’re gonna do is a preload
of script
. This way you can have a main script that will load with priority, and on script
href="carrega-depois.js"
you do the preload
for it to load after the first rendering is complete.
The value preload
of the attribute rel
of the element <link>
allows you to write declarative search requests in your element <head>
of HTML, specifying features that your pages will need right after loading.
Source: https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content
The value of the attribute rel="preload"
can be applied to various file formats including CSS, JS, fonts, images and more. You should define the corresponding attribute as depending on the file type. For CSS, the value should be as="style"
and for Javascript as="script"
. Article with more details
<link rel="preload" href="carrega-depois.js" as="script">
<script>
// Mais tarde, depois de alguma condição ter sido atendida, nós executamos o pré-carregado
// JavaScript inserindo uma tag <script> no DOM.
var usedLaterScript = document.createElement('script');
usedLaterScript.src = 'carrega-depois.js';
document.body.appendChild(usedLaterScript)
</script>
This is a very thorough article about preload
and prefetch
that might help you, I recommend you read: https://medium.com/reloading/preload-prefetch-and-priorities-in-chrome-776165961bbf
As a form of study I would recommend that you do both ways and test the result.
– Woss
So, it worked normal, in both cases... But scripts in the middle of the content, would be right? It gets weird, but it works.
– rbz