Why do includes in HTML files result in additional HTTP GET's?

Discussion in 'Apache' started by yohhan, Aug 13, 2007.

  1. #1
    Hopefully this is Apache related, but...

    Why is it when I try to include a stylesheet, or javascript in a file, it generates an additional HTTP GET for each include (as seen in my server logs)? If I have the following two lines of code in a php file called "test.php", I notice in my logs that 3 HTTP GETS are generated. One for test.php, one for the javascript file, and one for the stylesheet.

    
    <script language="JavaScript" type="text/javascript" src="/code.js"></script>
    <style type="text/css">@import url("/news.css");</style>
    
    Code (markup):
    It seems like a waste of resources to have my apache server making two additional HTTP connections to retrieve this file. Is this how it is supposed to work? Could it be a misconfiguration with my server, or is it likely a programming error?

    I should also mention that additional GET's do not happen when I do php includes.
     
    yohhan, Aug 13, 2007 IP
  2. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #2
    If you have a dynamic web page and a CSS file, the dynamic page is received and the cached CSS file is used. It ends up being more efficient with more requests. Also, remember that most of the time that is spent when a user loads a page is downloading the content, the requests themselves are relatively insignificant (to an extent, e.g. you would notice the effects of 10 requests to a server halfway across the world).

    PHP includes are different as that is server side, users do not download the PHP scripts, just the output from them.
     
    krt, Aug 13, 2007 IP
  3. bilal@revolutionhosting

    bilal@revolutionhosting Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    As "krt" noted, it can end up being more efficient with caching, etc.

    Additionally, the overhead of HTTP requests is not as terribly high as it used to be, with features like keepalive (so the same HTTP connection is used for multiple requests) and request pipelining (the client keeps sending requests without waiting for each response). So it is, in fact, generally recommended to keep your stylesheets and JavaScript scripts in separate files.
     
    bilal@revolutionhosting, Aug 14, 2007 IP
  4. yohhan

    yohhan Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the explanations, that helps me a lot.
     
    yohhan, Aug 14, 2007 IP