For example, I have a webpage with such structure in <head>, which is pretty common i guess:
<link rel="stylesheet" href="styles.css"> // here goes big css bundle
<script defer src="main.js"></script> // relatively small javascript bundle with defer attribute
And there is <span id="element"></span> on the page.
CSS bundle contains #element { display: none; }.
JS bundle contains (using jquery here):
$(document).ready(() => {
console.log($('#element').css('display'));
});
The result will be different from time to time. Sometimes JS executes earlier than CSS and the result is 'inline', sometimes JS executes later and the result is 'none' as I want it to be.
I want my JS bundle to be non-blocking so I use deffered attribute. I am not able to simply put my JS bundle in the end of a page because I use turbolinks and it doesn't allow me to do it.
window:load is not a best option too, because it will be fired when not only css but all resources will be downloaded including images.
So I want JS to be not-blocking and be executed after CSS to get consistent and predictable results. Maybe there is something I can do?