views:

47

answers:

2

I believe this would be a more CPU friendly method, can it be implemented with php ?, instead of gzipping content for every request, I compress the files once and serve those instead =).

+1  A: 

Yes, this is a sensible approach to save both bandwidth and connections. (You can enable gzip compression within Apache if so desired, but it's potentially worth doing this anyway as you've save connections.)

In essence, use a PHP function to check if the browser supports gzip compression. (If if doesn't you'll need to fetch the JavaScript/CSS as per normal.) If it does, you can simply point the JavaScript or CSS source location at a PHP script which is responsible for:

  1. Checking to see if there's a compressed version in place. (Simply output the existing 'on disk' if there is.)

  2. Creating a compressed version of the required files.

You'll also probably want to enable/disable this from a define/top level config (for testing purposes, etc.) As a suggestion, you could store the required CSS/JavaScript files paths in a set of arrays which could be used as a basis for creating the cache file or including the files in the traditional manner as a fallback.

I've written a solution along these lines in the past that created a file based on a hash of the required filenames. As such, the cache was automatically rebuilt if a different/additional file was included. (It also re-built the cache after 'n' hours, but that's only to keep things fresh if the filenames didn't change, but the content did.)

middaparka
hmm you mean sort of like facebook .. rite? "http://static.ak.fbcdn.net/rsrc.php/z3SLB/hash/9mv0ole0.css"
Akay
@Akay - Yes, along those lines.
middaparka
+3  A: 

Yes, this is quite easy to do with Apache.

Store the uncompressed and compressed files side by side. E.g.:

\-htdocs
  |-index.php
  |-javascript.js
  \-javascript.js.gz

Enable content negotiation in Apache. Use:

Options +MultiViews

Now when "/javascript" is requested, Apache will serve the gzipped version if the client declares it accepts it (through Accept-encoding).

Example of two HTTP requests (some headers omitted):

Client claims to accept gzip

GET /EP/Exames/2006-2007/exame2B HTTP/1.1
Host: lebm.geleia.net
Accept-Encoding: gzip, identity

HTTP/1.1 200 OK
Date: Fri, 13 Aug 2010 16:22:59 GMT
Content-Location: exame2B.nb.gz
Vary: negotiate,accept-encoding
TCN: choice
Last-Modified: Sun, 04 Feb 2007 15:33:53 GMT
ETag: "0-c9d-428a84de03a40;48db6d490abee"
Accept-Ranges: bytes
Content-Length: 3229
Content-Type: application/mathematica
Content-Encoding: gzip

‹áüÅE
(response continues)

Client does not claim to accept gzip

GET /EP/Exames/2006-2007/exame2B HTTP/1.1
Host: lebm.geleia.net
Accept-Encoding: identity

HTTP/1.1 200 OK
Date: Fri, 13 Aug 2010 16:23:14 GMT
Content-Location: exame2B.nb
Vary: negotiate,accept-encoding
TCN: choice
Last-Modified: Sun, 04 Feb 2007 15:33:53 GMT
ETag: "0-257f-428a84de03a40;48db6d490abee"
Accept-Ranges: bytes
Content-Length: 9599
Content-Type: application/mathematica

(************** Content-type: application/mathematica **************
CreatedBy='Mathematica 5.2'
(response continues)

See a more complete version here http://pastebin.com/TAwxpngX

Artefacto
intersting .. let me try this out.
Akay
=( it did not serve the compressed file =(Options -IndexesOptions +MultiViewsphp_flag magic_quotes_gpc offMy .htaccess ...
Akay
@Akay Sorry. I just tested and I was a little off. Your link must point to `javascript` only. No extension.
Artefacto