Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #3372

Re: including CSS & JS on only pages that need that CSS & JS

Message-ID <9489429.VONEhFrKcU@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2011-10-10 23:17 +0200
Subject Re: including CSS & JS on only pages that need that CSS & JS
Newsgroups comp.lang.php
References <e3d8f6c1-a54c-436b-930e-44dd89997f12@v18g2000pri.googlegroups.com> <qf0497lcimr000ah7nr9cnvjn7aurkdof1@4ax.com> <j6tc86$3oh$1@dont-email.me> <HUtkq.4097$%m.4010@newsfe05.iad>
Followup-To comp.lang.php

Followups directed to: comp.lang.php

Show all headers | View raw


Mr. B-o-B wrote:

> On 10/9/2011 6:54 PM, Jerry Stuckle cried from the depths of the abyss:
>> And files should be included using link tags, not directly included in
>> the source file directly (unless they are specific to this page - in
>> which case there is no need for conditional includes). That way file can
>> be cached by the browser and/or routers between the web server and the
>> client, decreasing both network traffic and server load.
> 
> Again, my original post fits these specs:
> 
> if (@isset $jerrys_css) {
> echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"jerry_css.css\"
> />";
> }
> 
> Then just set $jerry_css = 1 when needed.  Imagine that.  You are not
> decreasing network traffic, not increasing server load, cache is
> irrelevant as it's only cached in the pages that need it, you are using
> .js/.css via link tags.

This looks increasingly like blind teaching the blind.  As the subject is in 
fact partially on-topic here, since PHP can be used server-side:

0. Routers do not cache HTTP requests.  They usually work on lower levels
   of the TCP/IP stack, the Transport (TCP) and Internet Layer (IP), and
   usually see only TCP and IP packets.  Not only is a HTTP message usually
   split into several IP packets, but also is the payload transported by
   such a packet reduced so that the packet size does not exceed the MTU of
   the link, considering the header information of the packet as well.

   So we can safely dismiss the suggestion that the composition of Web
   resources could affect packet caching, if that even happens (routers
   usually cache *routing information*), as completely b…ogus.

1. You cannot "use .js via link tags".  Client-side scripts have to be
   included using `script' elements.

   It is the best current practice that all client-side scripts required
   for a document be included with one request, unless it is feasible and
   more efficient to load some of them dynamically when needed.  Which
   usually means that the request triggers a server-side script that
   composes the response for that one script.

2. It makes no difference to the number of requests whether you include
   other stylesheets with the CSS @import rule or the (X)HTML `link'
   element.  Cacheability is equal, too, as with the CSS directive the
   source code does not change as well.  [The same applies for scripts
   being loaded dynamically.]

   The main difference is in compatibility.  Rather ancient user agents
   would not support the `@import' rule but would support the `link'
   element:

   <http://w3development.de/css/hide_css_from_browsers/import/>

   [Much the same applies for dynamic script loading.  It usually requires
    XHR support or sufficient DOM support, while the plain `script' element
    is more compatible and reliable.]

3. It does make a difference to the loading speed of a Web site and the
   network traffic caused by it if you create one response from content of
   several CSS resources, as that is only one request.  Doing so is the best
   current practice.  As for caching, it rarely makes sense to exclude CSS
   rules from the generated response even if they are not used. 
   It really is a matter of loading speed/traffic vs. cacheability; usually,
   loading speed is considered to be more important, as local caches are
   usually large enough to handle the differences and visitors do not like
   to wait.

4. Requests for client-side scripts should be caused last, at least after
   the requests for stylesheets:

   <http://code.google.com/speed/page-
speed/docs/rtt.html#PutStylesBeforeScripts>


Bottom line: Fewer requests and smaller responses are better.

I have started writing a general resource builder in PHP to do that.  So far 
it works without problems for client-side scripts even in a production 
environment:

<http://PointedEars.de/websvn/filedetails.php?repname=JSX&path=%2Ftrunk%2Fbuilder.php>

(The ultimate goal is that it resolves dependencies of resources 
automatically.)

Constructive comments are welcome.


PointedEars
-- 
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
  -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

including CSS & JS on only pages that need that CSS & JS paris2venice <paris2venice@gmail.com> - 2011-10-08 23:51 -0700
  Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-09 08:34 -0400
    Re: including CSS & JS on only pages that need that CSS & JS paris2venice <paris2venice@gmail.com> - 2011-10-10 02:22 -0700
      Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-10 08:27 -0400
  Re: including CSS & JS on only pages that need that CSS & JS "Mr. B-o-B" <wtf.chuck@gmail.com> - 2011-10-09 08:46 -0500
    Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-09 13:52 -0400
      Re: including CSS & JS on only pages that need that CSS & JS Luuk <Luuk@invalid.lan> - 2011-10-09 20:03 +0200
        Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-09 15:08 -0400
  Re: including CSS & JS on only pages that need that CSS & JS Michael Joel <no@please.com> - 2011-10-09 16:31 -0400
    Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-09 19:54 -0400
      Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-09 20:03 -0400
      Re: including CSS & JS on only pages that need that CSS & JS "Mr. B-o-B" <wtf.chuck@gmail.com> - 2011-10-09 22:43 -0500
        Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-10 08:30 -0400
          Re: including CSS & JS on only pages that need that CSS & JS Michael Joel <no@please.com> - 2011-10-10 13:04 -0400
            Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-10 13:44 -0400
              Re: including CSS & JS on only pages that need that CSS & JS "Mr. B-o-B" <wtf.chuck@gmail.com> - 2011-10-10 21:35 -0500
                Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-10 23:04 -0400
                Re: including CSS & JS on only pages that need that CSS & JS "Mr. B-o-B" <wtf.chuck@gmail.com> - 2011-10-10 22:14 -0500
                Re: including CSS & JS on only pages that need that CSS & JS Michael Joel <no@please.com> - 2011-10-10 23:24 -0400
                Re: including CSS & JS on only pages that need that CSS & JS Tim Streater <timstreater@greenbee.net> - 2011-10-11 10:33 +0100
                Re: including CSS & JS on only pages that need that CSS & JS The Natural Philosopher <tnp@invalid.invalid> - 2011-10-11 11:29 +0100
                Re: including CSS & JS on only pages that need that CSS & JS Tim Streater <timstreater@greenbee.net> - 2011-10-11 12:00 +0100
            Re: including CSS & JS on only pages that need that CSS & JS The Natural Philosopher <tnp@invalid.invalid> - 2011-10-10 19:34 +0100
              Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-10 15:16 -0400
        Re: including CSS & JS on only pages that need that CSS & JS Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-10 23:17 +0200
          Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-10 17:36 -0400
  Re: including CSS & JS on only pages that need that CSS & JS Denis McMahon <denismfmcmahon@gmail.com> - 2011-10-11 09:09 +0000
    Re: including CSS & JS on only pages that need that CSS & JS Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-11 14:11 +0200
    Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-11 09:08 -0400
      Re: including CSS & JS on only pages that need that CSS & JS "Beauregard T. Shagnasty" <a.nony.mous@example.invalid> - 2011-10-11 13:29 +0000
        Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-11 13:14 -0400
      Re: including CSS & JS on only pages that need that CSS & JS Denis McMahon <denismfmcmahon@gmail.com> - 2011-10-11 19:14 +0000
        Re: including CSS & JS on only pages that need that CSS & JS Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-11 22:38 +0200
      Re: including CSS & JS on only pages that need that CSS & JS paris2venice <paris2venice@gmail.com> - 2011-10-11 17:48 -0700
        Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-11 21:51 -0400
          Re: including CSS & JS on only pages that need that CSS & JS paris2venice <paris2venice@gmail.com> - 2011-10-11 22:03 -0700
            Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-12 06:19 -0400
        Re: including CSS & JS on only pages that need that CSS & JS Hans Olo <doctor@vaderlishious.com> - 2011-10-11 22:11 -0500
          Re: including CSS & JS on only pages that need that CSS & JS Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-12 06:20 -0400

csiph-web