0

So, recently, after accruing quite a few js and css files, I began thinking about all of the http calls I was making.

Since I use PHP inlcude for the page headers and footers, every page was calling every script which was really inefficient. So I started using conditional statements in the pages with php to call only the needed files.

Then I thought, hey, wouldn't it be smarter to cut down on all of these http calls (even now every page has more than a few external js or css files) by making one huge css.php, and one huge js.php, and just include them in the header and footer respectively, and use conditional statements within those files?

The end result would be that those large files would be processed server side, and each page would get its css directly in the head and jss directly in the bottom of the body, with all of the code it needs and no more than that.

Is there any reason not to do this? Any thoughts in general on this idea? Is everyone else already doing this and I just missed the boat?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
sdo
  • 652
  • 3
  • 13
  • 1
    Nothing wrong with having php output a big js or css file, just be sure you've got appropriate cache headers set - some PHP installs make php output non-cacheable (since it's dynamic), so you might end up making things worse by having one huge uncacheable css rather than a few smallers ones that can be. – Marc B Nov 03 '11 at 18:34
  • Ah.. great point Marc - that's exactly the kind of insight I'm looking for. May I ask what I should be doing with my cache headers to rectify that possible issue? – sdo Nov 03 '11 at 18:36
  • http://stackoverflow.com/questions/1971721/how-to-use-http-cache-headers-with-php – Marc B Nov 03 '11 at 18:51

4 Answers4

1

If you're really worried about all of the http calls, consider taking a detailed look at PHP and AJAX (jQuery). If done right, it can be amazingly fast - and cool too. Also, remember your browser will cache your css/js in most cases.

jjwdesign
  • 3,272
  • 8
  • 41
  • 66
0

You can add following code to js.php (at first code line)

  <? 
   Header("content-type: application/x-javascript");

and this to css.php

  <? 
   header("Content-type: text/css");
Huseyin
  • 1,499
  • 2
  • 25
  • 39
0

Well, beside of doing HTTP calls, your browser makes them conditional. And gets in response from the server only a HEADER but not the acctual file, which is happily cashed in the browser already.

If you still thinks these HTTP calls are too heavy for your server - don't forget to implement the same behavior in your faked JS files.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

I would have liked to award this answer to Marc, but he didn't formally answer and everyone else was off base. Maybe I didn't ask the question clearly enough.

In any event, the answer is that this shouldn't really be done, as though it is efficient that each page gets only the css and js it needs and extraneous HTTP requests are avoided, the resulting files are always unique for each page and thus, not practically cache-able.

Thanks for everyone's input!

sdo
  • 652
  • 3
  • 13