GZipping CSS and JS files

2019-08-21 18:18发布

问题:

I'm using YSlow to improve the speed of my site, and I'm having trouble with the "compress components with gzip" grade. I have this in my .htaccess file:

SetOutputFilter DEFLATE

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/x-javascript

But YSlow is saying

There are 4 plain text components that should be sent compressed

* http://crewinyourcode.com/css/reset.css
* http://crewinyourcode.com/css/inner-pages/index.css
* http://crewinyourcode.com/script/css/jquery-ui-1.8.custom.css
* http://crewinyourcode.com/js/inner-pages/index.js

How can I gzip the css and js files?

Also...I don't have access to the httpd.conf file.

You can see the problem at http://crewinyourcode.com

UPDATE:

Added

AddType text/css .css
AddType application/x-javascript .js

to .htaccess and it didn't seem to help.

回答1:

Do you have AddType lines for .css and .js? It's pretty hard for Apache to automatically figure out the types by content alone -- they both look a lot like C code.

Try adding these lines to your .htaccess...

AddType text/css .css
AddType application/x-javascript .js


回答2:

Added, and it didn't help

Works for me, I'm now getting Content-Type: text/css and gzip encoding, which wasn't happening before due to the missing type mappings. That's something that should really be fixed server-wide though; it is a very broken server that cannot serve stylesheets and scripts with the right type.

Incidentally you should use text/javascript for scripts, for proper browser compatibility. application/javascript is proposed as ‘better’ for various nitpicking technical reasons, but it doesn't have the support so you can ignore that. x-javascript is long gone.