简体   繁体   中英

gzip javascript files to the browser faster?

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

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:

<IfModule mod_mime.c>
    AddCharset utf-8 .css
    AddCharset utf-8 .js
    AddEncoding gzip .gz
</IfModule>
<FilesMatch "(\.js|\.js\.gz)$">
    ForceType text/javascript
</FilesMatch>
<FilesMatch "(\.css|\.css\.gz)$">
    ForceType text/css
</FilesMatch>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM