GZipping CSS and JS files

我是研究僧i 提交于 2019-12-12 03:43:19

问题


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.



来源:https://stackoverflow.com/questions/2795493/gzipping-css-and-js-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!