开发者

Gzipped javascript not being served properly

I have a js file and a gzipped version of this js file.

The problem I am facing is trying to serve the gzipped version of this js file to the browsers that support it.

I don't know how to do that. If I add the js.gz to the current script element then it is not loaded and gives error.

How can I automatically serve the gzipped version of this js to the supported browsers.

开发者_开发知识库

Also I would like to restrict the gz serving the the folder /resources/widget/

I don't want to compress on the fly as i have around 1000 requests per second and it would take minutes to take down the server. Each js file is about 100KB and js.gz is about 16KB, so I would appreciate if I could be helped with my current files.


RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} “.*Safari.*” [OR]
RewriteCond %{HTTP:Accept-Encoding} !gzip
RewriteRule (.*)\.jgz$ $1\.js [L]

AddType “text/javascript;charset=UTF-8″ .jgz
AddEncoding gzip .jgz

rename your files from .gz to .jgz

OR use

<FilesMatch "\\.js.gz$">
  ForceType text/javascript
  Header set Content-Encoding: gzip
</FilesMatch>
<FilesMatch "\\.js$">
  RewriteEngine On
  RewriteCond %{HTTP_USER_AGENT} !".*Safari.*"
  RewriteCond %{HTTP:Accept-Encoding} gzip
  RewriteCond %{REQUEST_FILENAME}.gz -f
  RewriteRule (.*)\.js$ $1\.js.gz [L]
  ForceType text/javascript
</FilesMatch>


I have encountered problems with some browsers (notably Safari) not dealing appropriately when the file name extension was ".gz". We had to work around this by renaming the files to ".jgz"


You can use httacces to gzip for you, to browsers that support it:
see:
http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/


if you're using php :

In your htaccess file :

RewriteEngine On
RewriteRule (.*)\.js compress-js.php?file=$1.js [L]

compress-js.php :

ob_start("ob_gzhandler");
header("Content-type: text/javascript; charset: ISO-8859-1");
echo (file_get_contents ($file));  

That's all you need your files will be served in a compressed format.

Just add these 2 files .htaccess and compress-js.php to your js folder or wherever you store them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜