SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    220
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Please help! Compressing and caching external JS/CSS files

    ok...when i first used it, it did what it is supposed to do, with my FF webdeveloper addon, the view document size did show something like 5KB compressed and 15KB uncompressed.

    i created a PHP file named "packjs.php" with following codes:--

    PHP Code:
    <?php
    if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
        
    ob_start("ob_gzhandler");
        
    header("Content-Encoding: gzip");
    } else {
        
    ob_start();
    }

    header('Content-Type: text/javascript');
    $off 15768000//Number of seconds for the browser to cache the output
    header('Expires: ' gmdate('D, d M Y H:i:s'time() + $off) . ' GMT');
    header('Cache-Control: Public');
    header('Pragma: Public');

    $handle opendir(dirname(__file__)); //Assumes the PHP file is in the same folder as the JS files

    while (false !== ($f readdir($handle))) {
        if (
    substr($f, -3) == '.js') {
            
    readfile($f);
            echo 
    "\n";
        }
    }
    ob_flush();
    ?>
    then create a new folder to keep ALL of my JS files, like jquery, lightbox.....etc.

    i named the folder as "script"

    and put the file "packjs.php" into the "script" folder.

    now in my HTML file, instead of
    <HTML>
    <header>
    <script type="text/javascript" src="script/jquery.js"></script>
    <script type="text/javascript" src="script/lightbox.js"></script>
    </header>

    i need to put only
    <HTML>
    <header>
    <script type="text/javascript" src="script/packjs.php"></script>
    </header>

    The problem now is there is NO compression when i view with FF web developer addon anymore ever since i slightly mod the source code, but somehow forgot what i'd done with the code 1++ year ago...

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Have you checked to see if the addon if populating the $_SERVER['HTTP_ACCEPT_ENCODING'] variable and it contains what you are looking for?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    220
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Have you checked to see if the addon if populating the $_SERVER['HTTP_ACCEPT_ENCODING'] variable and it contains what you are looking for?
    did u mean there is something wrong with my addon or my apache config instead of the source code??

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    No idea, my thinking is, if you're not getting compressed js, the trigger in your code is that $_SERVER['HTTP_ACCEPT_ENCODING'] contains the string 'gzip'.

    Maybe, it's not there.

    Create a quick script to indicate this and perform a test.

    PHP Code:
    <?php
    if(false !== strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
    {
        echo 
    'gzip enabled';
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    220
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    No idea, my thinking is, if you're not getting compressed js, the trigger in your code is that $_SERVER['HTTP_ACCEPT_ENCODING'] contains the string 'gzip'.

    Maybe, it's not there.

    Create a quick script to indicate this and perform a test.

    PHP Code:
    <?php
    if(false !== strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
    {
        echo 
    'gzip enabled';
    }
    ?>
    yes, my FF show the string "gzip enabled" when running your script.

    so how?? what is wrong? could u test my script on your server or just localhost??

  6. #6
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your script works for me (Firefox 3.5). I dropped it into a directory full of .js files and WDTB reported 26 kb (76kb uncompressed).

    One problem with your script though is that it's going to output all of the JS in the standard alphabetical order that readdir will return. This isn't related to the compression problem, but if some scripts depend on others they need to be loaded in the correct order so that dependencies are present.

  7. #7
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    220
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    Your script works for me (Firefox 3.5). I dropped it into a directory full of .js files and WDTB reported 26 kb (76kb uncompressed).

    One problem with your script though is that it's going to output all of the JS in the standard alphabetical order that readdir will return. This isn't related to the compression problem, but if some scripts depend on others they need to be loaded in the correct order so that dependencies are present.
    that is why i rename my jquery.js into "ajquery.js"....


  8. #8
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    220
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ok... i just tested my script on both http://www.whatsmyip.org/http_compression/ and http://www.gidnetwork.com/tools/gzip-test.php

    result is "Yes", web page is compressed. Just my FF Web Developer addon doesn't report that.

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't need to send the content-encoding header, nor test if the browser supports it. php does that for you here.

  10. #10
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    It would probably be even better if you left the server itself to handle the compression as well as leave the server to handle the transferring. What I mean is pre-compile the JS and CSS and save those as static files.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •