Welcome to WebmasterWorld Guest from 34.204.173.36

Forum Moderators: phranque

Message Too Old, No Replies

Caching a PHP script loaded via jQuery's $.get()

     
1:17 am on Jun 1, 2018 (gmt 0)

Senior Member

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

joined:Mar 15, 2013
posts: 1205
votes: 120


I wasn't sure if this was a PHP question, jQuery, or .htaccess...

I load a PHP script for subnavigation using jQuery's $.get, mainly because the output is pretty lengthy (about 1.2kb) and it's not critical for the rest of the page to be usable. So it's one of the things I wait to load after the rest of the page has loaded.

The $.get looks like this:

$(function() {
$.get(home + '/includes/load_subnav.php', function(data) {
var vars = data.split(';');

for (var i = 0; i < vars.length; i++) {
var b = vars[i].split(' = ');
b[0] = b[0].trim();

subnavArr[decodeURIComponent(b[0])] = decodeURIComponent(b[1] || '');
}
});
});


And in the .htaccess I have:

<FilesMatch "load_subnav\.php|\.(ico|css|js|jpg|jpeg|png|gif)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>


But when I use Google Dev Tools > Network, it doesn't show that it's loading from cache. Why not? Is the function(data) messing it up? Or do I need to include a relative/absolute path in the .htaccess?

The load time is only about 370ms so it's not huge, but if I could get it to load from cache then it should drop down to around 50ms... and every little chip away helps! :-)
7:35 am on June 1, 2018 (gmt 0)

Full Member

joined:May 21, 2018
posts:276
votes: 72


Try using the "load" function instead of "get" - [api.jquery.com...]
5:01 pm on June 1, 2018 (gmt 0)

Senior Member

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

joined:Sept 25, 2005
posts:2091
votes: 370


I don't think the GET request is different with load() vs get().

Try setting a Last-Modified header for load_subnav.php. If you do it from PHP, you could use filemtime(), but if you want to avoid the call you could also set it manually.

I feel like you're entering the realm of micro-optimizations ;-)

The sub-navigation is not important for search bots?
1:39 am on June 2, 2018 (gmt 0)

Senior Member

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

joined:Mar 15, 2013
posts: 1205
votes: 120


I feel like you're entering the realm of micro-optimizations ;-)

I am, using the logic that 15 minutes of research and coding now might turn in to extra $$$ at the end of the month :-D But I'm also using it as a learning tool, so the answer given now might save me work in the future.

The sub-navigation is not important for search bots?

Hang on, I thought that bots could read Javascript now, and would thus follow the links?
7:52 am on June 2, 2018 (gmt 0)

Senior Member

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

joined:Sept 25, 2005
posts:2091
votes: 370


They can, and they might, but that's not a certainty. If they end up crawling load_subnav.php, they'll find the links, but they may not be treated the same way as when they'd be embedded in the HTML. Keep in mind you're doing something fairly unusual here.
6:17 pm on June 2, 2018 (gmt 0)

Senior Member

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

joined:Mar 15, 2013
posts: 1205
votes: 120


Hmm... OK, I'll keep that in mind. I DO duplicate the subnavigation in the HTML in the footer of the page, though, so the embedded links are still there, but maybe not as prominent. If search engine placement gets worse then I might have to undo this :-/
9:33 pm on June 2, 2018 (gmt 0)

Senior Member

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

joined:Sept 25, 2005
posts:2091
votes: 370


Is it that big of an improvement when you defer it? If load_subnav.php is 1.2 KB as a separate file, it's probably less than that when embedded into the HTML (tighter compression). Perhaps you can compress the code further and/or minify the HTML?

Did you try the Last-Modified header, though?
12:57 am on June 4, 2018 (gmt 0)

Senior Member

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

joined:Mar 15, 2013
posts: 1205
votes: 120


I'm pretty sure that the 1.2kb was the compressed output, I was using Google Dev Tool to come up with that number. The PHP script itself is only 588 bytes, but it has two loops so the output is pretty large.

Based on the original site (which has the same navigation links, but not as a separate script), the uncompressed size would be around 32kb. But that was using tables and stuff, so if I rebuilt it then it would probably be closer to 20kb uncompressed.

I haven't tried Last-Modified yet, I'm working on another issue right now... I'll post back when I try it, though :-)
7:24 am on June 4, 2018 (gmt 0)

Senior Member

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

joined:Sept 25, 2005
posts:2091
votes: 370


My point was that the compression of the contents of subnav_load.php as a separate file is not as efficient as when those same contents would be embedded in the main document, especially since you note that the code is actually duplicated within the document. The DEFLATE algorithm looks for duplicate strings, and is likely to find more of those in the main document than in subnav_load.php. So if subnav_load.php is 1.2 KB as a separate file, it is very likely to add less than that to the main document if you were to embed it.