Forum Moderators: phranque

Message Too Old, No Replies

Should I use a separate stylesheet within an Ajax loaded script

or, caching an Ajax loaded script

         

csdude55

5:25 pm on Feb 13, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This question crosses Javascript and CSS but is really a question about caching, so I put it in General instead... hope that's OK!

I've been moving non-essential below-the-fold content to load via Ajax (jQuery) so that I can control the order in which everything loads. This is allowing important content to load much faster, which is great :-)

Several of these Ajax scripts have CSS that's only used for the data within that script; combined, about 10kb worth of CSS. Since they load after everything else, I was creating a second stylesheet just for them so that the main stylesheet can load a smidge faster.

It's my understanding that the value to using a separate stylesheet is that it is loaded once and cached, so the second page load is faster.

So the question is, since these are Ajax scripts are already being cached, is there an advantage to putting their CSS in a separate stylesheet? Or is the Ajax script already cached on its first load, which would mean that a separate stylesheet would just be an additional HTTP connection for no advantage? I'm thinking it's the second one (not using a separate stylesheet), but I'm hoping for some sort of verification.

To explain via code:


<!-- MAIN PAGE -->

<!-- load main stylesheet -->
<link rel="stylesheet" href="//foo.com/main_styles.css">

<!-- load jQuery -->
<script src='//link-to/jquery.js'></script>

<script>

// Ajax script
$.fn.ajax = function(url) {
if (url)
this.load(url);

else
this.hide();
}

$(function() {

// run this after everything else, push the results inside of <footer></footer>
$('footer').ajax('//foo.com/ajax_script.php');
});
</script>

<body>

<footer></footer>

</body>


<!-- AJAX SCRIPT -->
<?php
echo <<<EOF

<!-- the question is, should this be here? Or in a separate ajax_styles.css? -->
<!-- imagine 10kb worth of CSS -->
<style>
p.class {
background: #800;
}
</style>

<p>
This is the footer
</p>

EOF;
?>

lammert

5:42 pm on Feb 13, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Are you serving over HTTP/2 or HTTP/1.1? With the latter, the overhead needed for the extra style sheet request will add more time delay than just loading all CSS in one sheet. HTTP/2 can load multiple resources asynchronously which makes it less of a problem.

csdude55

9:27 pm on Feb 13, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



My server doesn't support HTTP/2, so I guess 1.1... I am using HTTPS, though, of course

lammert

9:53 pm on Feb 13, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The reason to split files and tune browser cache headers is mainly because of historic reasons. Originally it helped to optimize the way browsers and http servers exchanged resources, by optimizing parallel transfers over multiple channels and avoiding blocking between separate resources. With the new HTTP/2 and proposed HTTP/3 protocol, all the micromanagement involved to gain a few milliseconds by splitting files, managing cache headers etc is obsolete.

If you are using Apache with version number 2.4.26 or higher (older versions have security issues), you may try to switch it on and then decide if there is still a need to optimize the load procedure of 10kb small CSS files. I flipped the switch a few months ago and in a few minutes after the switch, I realized how much time I had wasted in the past doing all those small tweaks for just a few milliseconds left and right.

csdude55

10:21 pm on Feb 13, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm using Apache 2.4.41, but HTTP/2 requires PHP 7.x and I still have PHP v5.6.40... mainly because most of my sites use MySQL, and 7.x requires MySQLi or PDO. So I can't update to HTTP/2 until I update PHP, and I can't update PHP until I've rebuilt all of my sites for MySQLi :'-(

It feels like a huge oversight to have a major PHP release that doesn't at least have some sort of patch to allow for pre-MySQLi, but that's the way it goes, I guess