[jQuery] Re: stupid dev tricks: gzipping jQuery without mod_deflate
Right, just like that, but written in Java. I also do CSS, but that's just a matter of a few regexps. --Erik On 8/15/07, polyrhythmic <[EMAIL PROTECTED]> wrote: > > @Erik: (Damn this Groups post delay...) Minify does exactly that, > but with CSS too. Minifying, caching, and single-request serving. > > On Aug 15, 2:32 pm, "Erik Beeson" <[EMAIL PROTECTED]> wrote: > > I cache the packed versions. Actually, I concatenate all of the scripts that > > I need for a page, minify them (used to use packer, now I use YUImin), and > > then cache that, all on the fly. So I have one script tag like: > > > > > > > > On the first request, I merge the three scripts, minify them, write them out > > to a cache file, and store the cache file path with the script names. Next > > time I get these same scripts in this order, I serve out the cached version. > > This has quite a few benefits: it allows me to have whatever scripts I need > > on a given page, it keeps my scripts readable on the server while still > > compressing them for the client, and it reduces the number of HTTP requests > > necessary to load all of the scripts. > > > > The only real downside is you may lose a little performance from things not > > caching as much as they would if they were in separate files, so I don't > > serve up jquery.js/interface.js/ext.js this way hoping that the browser will > > cache them. > > > > --Erik > > > > On 8/15/07, Ganeshji Marwaha <[EMAIL PROTECTED]> wrote: > > > > > > > > > Very cool... I will start using this technique right away... > > > > > But my only concern is, since this technique compresses the file everytime > > > it is requested, isn't it an overkill on the server's CPU? > > > > > -GTG > > > > > On 8/15/07, Stephan Beal <[EMAIL PROTECTED]> wrote: > > > > > > Hi, all! > > > > > > i'm working on re-designing the web site for my mother's company, > > > > which was horribly neglected/abused by the previous webmaster, and i > > > > came across an interesting problem... > > > > > > A part of the refactoring is to use jQuery for parts of the site. > > > > However, since all of the pages use the same site layout template > > > > (which includes the headers/script tags) yet most of the site won't > > > > actually use the JS features, i wanted to make the jQ download as tiny > > > > as possible. > > > > > > Unfortunately, i don't have admin rights on my server so i cannot > > > > activate mod_gzip/mod_deflate to gzip the stuff on the fly. But here's > > > > an easy workaround... > > > > > > Create a PHP file called jquery.php: > > > > > > > > > ob_start( 'ob_gzhandler' ); > > > > echo join('',file('jquery-1.1.3.1.pack.js')); > > > > ob_end_flush(); > > > > ?> > > > > > > Now, in the main site layout template i have: > > > > > > > > > > > > Firebug confirms that the jQ transfer is then 12k, which is tolerable > > > > for my purposes. > > > > > > It would be only a tiny amount of extra work to integrate the PHP port > > > > of Dean Edwards' packer, such that the packing is done each time > > > > jquery.php is called, but that seems like overkill to me. > > > > > > This approach could just as easily be used to combine all required JS > > > > scripts on the fly (just be sure to insert a ';' after each one to > > > > accommodate scripts which don't have them), then gzip them, to help > > > > reduce the overall download overhead. > >
[jQuery] Re: stupid dev tricks: gzipping jQuery without mod_deflate
@Erik: (Damn this Groups post delay...) Minify does exactly that, but with CSS too. Minifying, caching, and single-request serving. On Aug 15, 2:32 pm, "Erik Beeson" <[EMAIL PROTECTED]> wrote: > I cache the packed versions. Actually, I concatenate all of the scripts that > I need for a page, minify them (used to use packer, now I use YUImin), and > then cache that, all on the fly. So I have one script tag like: > > > > On the first request, I merge the three scripts, minify them, write them out > to a cache file, and store the cache file path with the script names. Next > time I get these same scripts in this order, I serve out the cached version. > This has quite a few benefits: it allows me to have whatever scripts I need > on a given page, it keeps my scripts readable on the server while still > compressing them for the client, and it reduces the number of HTTP requests > necessary to load all of the scripts. > > The only real downside is you may lose a little performance from things not > caching as much as they would if they were in separate files, so I don't > serve up jquery.js/interface.js/ext.js this way hoping that the browser will > cache them. > > --Erik > > On 8/15/07, Ganeshji Marwaha <[EMAIL PROTECTED]> wrote: > > > > > Very cool... I will start using this technique right away... > > > But my only concern is, since this technique compresses the file everytime > > it is requested, isn't it an overkill on the server's CPU? > > > -GTG > > > On 8/15/07, Stephan Beal <[EMAIL PROTECTED]> wrote: > > > > Hi, all! > > > > i'm working on re-designing the web site for my mother's company, > > > which was horribly neglected/abused by the previous webmaster, and i > > > came across an interesting problem... > > > > A part of the refactoring is to use jQuery for parts of the site. > > > However, since all of the pages use the same site layout template > > > (which includes the headers/script tags) yet most of the site won't > > > actually use the JS features, i wanted to make the jQ download as tiny > > > as possible. > > > > Unfortunately, i don't have admin rights on my server so i cannot > > > activate mod_gzip/mod_deflate to gzip the stuff on the fly. But here's > > > an easy workaround... > > > > Create a PHP file called jquery.php: > > > > > > ob_start( 'ob_gzhandler' ); > > > echo join('',file('jquery-1.1.3.1.pack.js')); > > > ob_end_flush(); > > > ?> > > > > Now, in the main site layout template i have: > > > > > > > > Firebug confirms that the jQ transfer is then 12k, which is tolerable > > > for my purposes. > > > > It would be only a tiny amount of extra work to integrate the PHP port > > > of Dean Edwards' packer, such that the packing is done each time > > > jquery.php is called, but that seems like overkill to me. > > > > This approach could just as easily be used to combine all required JS > > > scripts on the fly (just be sure to insert a ';' after each one to > > > accommodate scripts which don't have them), then gzip them, to help > > > reduce the overall download overhead.
[jQuery] Re: stupid dev tricks: gzipping jQuery without mod_deflate
@Stephen: Nice trick, I'll be adding it to my bag! To all: If you're already using PHP includes and JSMin why not go all the way to Minify? I use Minify ( http://code.google.com/p/minify/ , thx rgrove) for my JS & CSS. With jQuery, nearly all of us are using more than one JavaScript include, minify combines and compresses, and the pages are blazing fast. I'm not a numbers nut so I can't say quantitatively, but qualitatively just seeing my page load near- instantly over GPRS on an iPhone confirmed my choice. Plus for those of us (like me) on shared hosts with PHP4, Vulgarisoverip has done a PHP4 translation: http://www.vulgarisoip.com/2007/06/21/minify-your-external-javascript-and-css-with-php/ I have mentioned this before but Minify seems underappreciated. Is there any reason NOT to use it? Charles doublerebel.com On Aug 15, 6:53 am, "Joel Birch" <[EMAIL PROTECTED]> wrote: > On 8/15/07, Stephan Beal <[EMAIL PROTECTED]> wrote: > > > @Joel/Brandon: > > > after more experimentation, i am getting better compression if i use > > the YUImin and ob_gzhandler. The difference is minimal compared to > > jsmin, but there is a difference. e.g. > > > jquery 1.1.3.1: > > YUImin + gzip: 10896 bytes > > jsmin + gzip: 11505 > > packer + gzip: 11486 > > Good to know. Thanks! > > Joel Birch.
[jQuery] Re: stupid dev tricks: gzipping jQuery without mod_deflate
Ah, good to know. In my case, we're running on our own servers, so that isn't too big an issue. Also, we're using Java, and store the cache files in the webapp's temporary directory, and set them to delete on exit (java.io.File.deleteOnExit()), so they get trashed when we restart the server. But thanks for pointing it out for those dealing with a shared hosting environment. --Erik On 8/15/07, Stephan Beal <[EMAIL PROTECTED]> wrote: > > On Aug 15, 11:32 pm, "Erik Beeson" <[EMAIL PROTECTED]> wrote: > > I cache the packed versions. Actually, I concatenate all of the scripts that > > I need for a page, minify them (used to use packer, now I use YUImin), and > > then cache that, all on the fly. So I have one script tag like: > > The problem with caching is that it fails on some hosting providers. > For example, if i did this on the SourceForge servers, the cached file > would be owned by some web user other than me, which means my account > cannot remove the file. Writing files from a web server is often > problematic vis-a-vis file access rights, especially when your account > is not the one which apache runs as. It is possible to tell apache, > with the right module(s?), to run each virtual host as a different > user, but it's not always possible to set this up (e.g. SourceForge > sites). > > >
[jQuery] Re: stupid dev tricks: gzipping jQuery without mod_deflate
On Aug 15, 11:08 am, Stephan Beal <[EMAIL PROTECTED]> wrote: ... > This approach could just as easily be used to combine all required JS > scripts on the fly (just be sure to insert a ';' after each one to > accommodate scripts which don't have them), then gzip them, to help > reduce the overall download overhead. A small follow-up: After doing some reading, it turns out that PHP has a more efficient manner of reading text files (which uses mem-mapping to avoid extra string copying), called file_get_contents(). So here's a slightly expanded version which uses that function (using fopen() and friends would probably be optimal, though) and takes an array of JS files, effectively concatenating the results into one script: Happy hacking!
[jQuery] Re: stupid dev tricks: gzipping jQuery without mod_deflate
On Aug 15, 11:32 pm, "Erik Beeson" <[EMAIL PROTECTED]> wrote: > I cache the packed versions. Actually, I concatenate all of the scripts that > I need for a page, minify them (used to use packer, now I use YUImin), and > then cache that, all on the fly. So I have one script tag like: The problem with caching is that it fails on some hosting providers. For example, if i did this on the SourceForge servers, the cached file would be owned by some web user other than me, which means my account cannot remove the file. Writing files from a web server is often problematic vis-a-vis file access rights, especially when your account is not the one which apache runs as. It is possible to tell apache, with the right module(s?), to run each virtual host as a different user, but it's not always possible to set this up (e.g. SourceForge sites).
[jQuery] Re: stupid dev tricks: gzipping jQuery without mod_deflate
On Aug 15, 11:02 pm, "Ganeshji Marwaha" <[EMAIL PROTECTED]> wrote: > Very cool... I will start using this technique right away... > > But my only concern is, since this technique compresses the file everytime > it is requested, isn't it an overkill on the server's CPU? Getting this level of transparency is worth the small computational cost, IMO. It is essentially the same as Apache using mod_deflate (Apache 2.x) or mod_gzip (Apache 1.x). The default compression level for the gzip algorithm performs REALLY quickly.
[jQuery] Re: stupid dev tricks: gzipping jQuery without mod_deflate
I cache the packed versions. Actually, I concatenate all of the scripts that I need for a page, minify them (used to use packer, now I use YUImin), and then cache that, all on the fly. So I have one script tag like: On the first request, I merge the three scripts, minify them, write them out to a cache file, and store the cache file path with the script names. Next time I get these same scripts in this order, I serve out the cached version. This has quite a few benefits: it allows me to have whatever scripts I need on a given page, it keeps my scripts readable on the server while still compressing them for the client, and it reduces the number of HTTP requests necessary to load all of the scripts. The only real downside is you may lose a little performance from things not caching as much as they would if they were in separate files, so I don't serve up jquery.js/interface.js/ext.js this way hoping that the browser will cache them. --Erik On 8/15/07, Ganeshji Marwaha <[EMAIL PROTECTED]> wrote: > > Very cool... I will start using this technique right away... > > But my only concern is, since this technique compresses the file everytime > it is requested, isn't it an overkill on the server's CPU? > > -GTG > > On 8/15/07, Stephan Beal <[EMAIL PROTECTED]> wrote: > > > > > > Hi, all! > > > > i'm working on re-designing the web site for my mother's company, > > which was horribly neglected/abused by the previous webmaster, and i > > came across an interesting problem... > > > > A part of the refactoring is to use jQuery for parts of the site. > > However, since all of the pages use the same site layout template > > (which includes the headers/script tags) yet most of the site won't > > actually use the JS features, i wanted to make the jQ download as tiny > > as possible. > > > > Unfortunately, i don't have admin rights on my server so i cannot > > activate mod_gzip/mod_deflate to gzip the stuff on the fly. But here's > > an easy workaround... > > > > Create a PHP file called jquery.php: > > > > > ob_start( 'ob_gzhandler' ); > > echo join('',file('jquery-1.1.3.1.pack.js')); > > ob_end_flush(); > > ?> > > > > Now, in the main site layout template i have: > > > > > > > > Firebug confirms that the jQ transfer is then 12k, which is tolerable > > for my purposes. > > > > It would be only a tiny amount of extra work to integrate the PHP port > > of Dean Edwards' packer, such that the packing is done each time > > jquery.php is called, but that seems like overkill to me. > > > > This approach could just as easily be used to combine all required JS > > scripts on the fly (just be sure to insert a ';' after each one to > > accommodate scripts which don't have them), then gzip them, to help > > reduce the overall download overhead. > > > > >
[jQuery] Re: stupid dev tricks: gzipping jQuery without mod_deflate
Very cool... I will start using this technique right away... But my only concern is, since this technique compresses the file everytime it is requested, isn't it an overkill on the server's CPU? -GTG On 8/15/07, Stephan Beal <[EMAIL PROTECTED]> wrote: > > > Hi, all! > > i'm working on re-designing the web site for my mother's company, > which was horribly neglected/abused by the previous webmaster, and i > came across an interesting problem... > > A part of the refactoring is to use jQuery for parts of the site. > However, since all of the pages use the same site layout template > (which includes the headers/script tags) yet most of the site won't > actually use the JS features, i wanted to make the jQ download as tiny > as possible. > > Unfortunately, i don't have admin rights on my server so i cannot > activate mod_gzip/mod_deflate to gzip the stuff on the fly. But here's > an easy workaround... > > Create a PHP file called jquery.php: > >ob_start( 'ob_gzhandler' ); > echo join('',file('jquery-1.1.3.1.pack.js')); > ob_end_flush(); > ?> > > Now, in the main site layout template i have: > > > > Firebug confirms that the jQ transfer is then 12k, which is tolerable > for my purposes. > > It would be only a tiny amount of extra work to integrate the PHP port > of Dean Edwards' packer, such that the packing is done each time > jquery.php is called, but that seems like overkill to me. > > This approach could just as easily be used to combine all required JS > scripts on the fly (just be sure to insert a ';' after each one to > accommodate scripts which don't have them), then gzip them, to help > reduce the overall download overhead. > >
[jQuery] Re: stupid dev tricks: gzipping jQuery without mod_deflate
On 8/15/07, Stephan Beal <[EMAIL PROTECTED]> wrote: > @Joel/Brandon: > > after more experimentation, i am getting better compression if i use > the YUImin and ob_gzhandler. The difference is minimal compared to > jsmin, but there is a difference. e.g. > > jquery 1.1.3.1: > YUImin + gzip: 10896 bytes > jsmin + gzip: 11505 > packer + gzip: 11486 Good to know. Thanks! Joel Birch.
[jQuery] Re: stupid dev tricks: gzipping jQuery without mod_deflate
Before ob_gzhandler() actually sends compressed data, it determines what type of content encoding the browser will accept ("gzip", "deflate" or none at all) and will return its output accordingly. >From http://uk.php.net/ob_gzhandler On Aug 15, 2:33 pm, xavier <[EMAIL PROTECTED]> wrote: > This means that : > 1) you assume all clients are able to deal with compressed pages > 2) your server is going to compress it for each visitor. > 3) the headers might or might not be properly dealing with its type. > > With mod_rewrite, they are nice tricks to have a compressed file and > serve it instead of the normal file if needed. > > Have you tried compressing the js file and send it instead of the file > without using mod_compress/mod_rewrite ? > > X+ > > On Aug 15, 11:08 am, Stephan Beal <[EMAIL PROTECTED]> wrote: > > > Hi, all! > > > i'm working on re-designing the web site for my mother's company, > > which was horribly neglected/abused by the previous webmaster, and i > > came across an interesting problem... > > > A part of the refactoring is to use jQuery for parts of the site. > > However, since all of the pages use the same site layout template > > (which includes the headers/script tags) yet most of the site won't > > actually use the JS features, i wanted to make the jQ download as tiny > > as possible. > > > Unfortunately, i don't have admin rights on my server so i cannot > > activate mod_gzip/mod_deflate to gzip the stuff on the fly. But here's > > an easy workaround... > > > Create a PHP file called jquery.php: > > > > ob_start( 'ob_gzhandler' ); > > echo join('',file('jquery-1.1.3.1.pack.js')); > > ob_end_flush(); > > ?> > > > Now, in the main site layout template i have: > > > > > > Firebug confirms that the jQ transfer is then 12k, which is tolerable > > for my purposes. > > > It would be only a tiny amount of extra work to integrate the PHP port > > of Dean Edwards' packer, such that the packing is done each time > > jquery.php is called, but that seems like overkill to me. > > > This approach could just as easily be used to combine all required JS > > scripts on the fly (just be sure to insert a ';' after each one to > > accommodate scripts which don't have them), then gzip them, to help > > reduce the overall download overhead.
[jQuery] Re: stupid dev tricks: gzipping jQuery without mod_deflate
On Aug 15, 3:33 pm, xavier <[EMAIL PROTECTED]> wrote: > This means that : > 1) you assume all clients are able to deal with compressed pages No - PHP does that negotiation automatically (supposedly) and ignores the gzip if it thinks the client can't handle it. > 2) your server is going to compress it for each visitor. Why wouldn't they? > 3) the headers might or might not be properly dealing with its type. i don't understand what you mean by this. The HTTP response header from the PHP script? > With mod_rewrite, they are nice tricks to have a compressed file and > serve it instead of the normal file if needed. > > Have you tried compressing the js file and send it instead of the file > without using mod_compress/mod_rewrite ? i haven't tried that because i wouldn't expect sub-optimal browsers to be able to grok it (by that i mean MSIE/Safari/Opera). Since i don't have those browsers to test, i don't want to try anything too tricky which might break those browsers. @Joel/Brandon: after more experimentation, i am getting better compression if i use the YUImin and ob_gzhandler. The difference is minimal compared to jsmin, but there is a difference. e.g. jquery 1.1.3.1: YUImin + gzip: 10896 bytes jsmin + gzip: 11505 packer + gzip: 11486
[jQuery] Re: stupid dev tricks: gzipping jQuery without mod_deflate
This means that : 1) you assume all clients are able to deal with compressed pages 2) your server is going to compress it for each visitor. 3) the headers might or might not be properly dealing with its type. With mod_rewrite, they are nice tricks to have a compressed file and serve it instead of the normal file if needed. Have you tried compressing the js file and send it instead of the file without using mod_compress/mod_rewrite ? X+ On Aug 15, 11:08 am, Stephan Beal <[EMAIL PROTECTED]> wrote: > Hi, all! > > i'm working on re-designing the web site for my mother's company, > which was horribly neglected/abused by the previous webmaster, and i > came across an interesting problem... > > A part of the refactoring is to use jQuery for parts of the site. > However, since all of the pages use the same site layout template > (which includes the headers/script tags) yet most of the site won't > actually use the JS features, i wanted to make the jQ download as tiny > as possible. > > Unfortunately, i don't have admin rights on my server so i cannot > activate mod_gzip/mod_deflate to gzip the stuff on the fly. But here's > an easy workaround... > > Create a PHP file called jquery.php: > >ob_start( 'ob_gzhandler' ); > echo join('',file('jquery-1.1.3.1.pack.js')); > ob_end_flush(); > ?> > > Now, in the main site layout template i have: > > > > Firebug confirms that the jQ transfer is then 12k, which is tolerable > for my purposes. > > It would be only a tiny amount of extra work to integrate the PHP port > of Dean Edwards' packer, such that the packing is done each time > jquery.php is called, but that seems like overkill to me. > > This approach could just as easily be used to combine all required JS > scripts on the fly (just be sure to insert a ';' after each one to > accommodate scripts which don't have them), then gzip them, to help > reduce the overall download overhead.
[jQuery] Re: stupid dev tricks: gzipping jQuery without mod_deflate
On 8/15/07, Brandon Aaron <[EMAIL PROTECTED]> wrote: > > Yeah, the packer does a good job initially but doesn't do a good job of > getting gzipped. At least with my testing, I'm able to get a better file > size by using the min version + gzip. jQuery is only around 11k doing it > that way. > > -- > Brandon Aaron I've noticed this too. I'm sticking with jsMin and gzip, myself. Joel Birch
[jQuery] Re: stupid dev tricks: gzipping jQuery without mod_deflate
Yeah, the packer does a good job initially but doesn't do a good job of getting gzipped. At least with my testing, I'm able to get a better file size by using the min version + gzip. jQuery is only around 11k doing it that way. -- Brandon Aaron On 8/15/07, Stephan Beal <[EMAIL PROTECTED]> wrote: > > > On Aug 15, 2:33 pm, "Brandon Aaron" <[EMAIL PROTECTED]> wrote: > > Thanks for sharing this. Use the minified version instead of the packed > > version for even better file size savings. :) > > i experimented on that. i expected the new YUI minifier to give better > compression (as it does on all of my plugins), but it only compresses > jQ to 30k. i got the best jQ compression using a PHP port of the > Edwards Packer: > > http://wanderinghorse.net/computing/javascript/#packers > > In any case, the different pack/min techniques will give different > results on different inputs. On very small JS files MIN is normally > better because of the PACK overhead (just under 200 bytes, i think). > On large files, PACK tends to beat out MIN, but this isn't always so. > YUI minifier is damned good, at least on my code, but it seems to not > be the best packer for jQ. Anyway... the point is simply to experiment > and find the best one for your purposes. A couple K either way > probably isn't going to kill anyone. > >
[jQuery] Re: stupid dev tricks: gzipping jQuery without mod_deflate
On Aug 15, 2:33 pm, "Brandon Aaron" <[EMAIL PROTECTED]> wrote: > Thanks for sharing this. Use the minified version instead of the packed > version for even better file size savings. :) i experimented on that. i expected the new YUI minifier to give better compression (as it does on all of my plugins), but it only compresses jQ to 30k. i got the best jQ compression using a PHP port of the Edwards Packer: http://wanderinghorse.net/computing/javascript/#packers In any case, the different pack/min techniques will give different results on different inputs. On very small JS files MIN is normally better because of the PACK overhead (just under 200 bytes, i think). On large files, PACK tends to beat out MIN, but this isn't always so. YUI minifier is damned good, at least on my code, but it seems to not be the best packer for jQ. Anyway... the point is simply to experiment and find the best one for your purposes. A couple K either way probably isn't going to kill anyone.
[jQuery] Re: stupid dev tricks: gzipping jQuery without mod_deflate
Thanks for sharing this. Use the minified version instead of the packed version for even better file size savings. :) -- Brandon Aaron On 8/15/07, Stephan Beal <[EMAIL PROTECTED]> wrote: > > > Hi, all! > > i'm working on re-designing the web site for my mother's company, > which was horribly neglected/abused by the previous webmaster, and i > came across an interesting problem... > > A part of the refactoring is to use jQuery for parts of the site. > However, since all of the pages use the same site layout template > (which includes the headers/script tags) yet most of the site won't > actually use the JS features, i wanted to make the jQ download as tiny > as possible. > > Unfortunately, i don't have admin rights on my server so i cannot > activate mod_gzip/mod_deflate to gzip the stuff on the fly. But here's > an easy workaround... > > Create a PHP file called jquery.php: > >ob_start( 'ob_gzhandler' ); > echo join('',file('jquery-1.1.3.1.pack.js')); > ob_end_flush(); > ?> > > Now, in the main site layout template i have: > > > > Firebug confirms that the jQ transfer is then 12k, which is tolerable > for my purposes. > > It would be only a tiny amount of extra work to integrate the PHP port > of Dean Edwards' packer, such that the packing is done each time > jquery.php is called, but that seems like overkill to me. > > This approach could just as easily be used to combine all required JS > scripts on the fly (just be sure to insert a ';' after each one to > accommodate scripts which don't have them), then gzip them, to help > reduce the overall download overhead. > >
[jQuery] Re: stupid dev tricks: gzipping jQuery without mod_deflate
Very nice! I have never bothered messing with compression before but now that I'm writing a rather large web app i need to make it as lean as possible. According to firebug I trimmed off 20% of my js size by piping all the JS through this! :)