Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

gzip javascript files to the browser faster?

gzip javascript files to the browser faster?

Problem

I'm using node.js (not socket.io, im using ws and flash fall backs) I would like some opinions on serving js files as gzipped / zipped files. I have googled but I'm not sure I know enough to understand. So before I continue to google I think it is a good idea to see what you have to say about my cheddar cheese idea and perhaps help me clean my brain of silly questions.

  1. what does the Browser do with zipped js files? (I think nothing, it will download it to your computer as a zip file).

  2. is there something for unzipping and adding js as a script tag (I think still it will be from the Server to 'do you want to keep this file, this file may harm your computer [yes][no]?')

  3. is this someting todo with htaccess or does the client do it?

thanks

Problem courtesy of: Ben Muircroft

Solution

What the browser does with the files depends on the http headers that the server provides in the response. For instance, we serve our gzipped scripts with the following headers:

Content-Type: text/javascript
Content-Encoding: gzip

Style sheets are delivered the same way, but with content type text/css. The browser automatically unzips them and then processes them exactly as if they were delivered unzipped. This includes Ajax requests.

For static, gzipped files, we use the .htaccess file on our Apache server for this. Here are the relevant lines:


    AddCharset utf-8 .css
    AddCharset utf-8 .js
    AddEncoding gzip .gz

    ForceType text/javascript

    ForceType text/css

For dynamic content, the server script generates the headers as well as the response.

Solution courtesy of: Ted Hopp

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

gzip javascript files to the browser faster?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×