如何为css / js文件设置缓存 [英] how to set cache for css/js file

查看:334
本文介绍了如何为css / js文件设置缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用缓存的css文件和js文件,我在网站中使用。
我的网站运行在共享托管服务器。没有什么可以做到与服务器。

I have to use the cache for the css files and js file which i used in the site. my site running in a shared hosting server. nothing can be done with server.

因此,使用缓存和压缩js和css文件的解决方案。

so what could be the solution for use cache and compression for js and css files.

推荐答案

如果您使用Apache,我会从这里开始。

If you are using Apache, I would start here.

  • http://www.askapache.com/htaccess/htaccess.html
  • http://www.askapache.com/htaccess/speed-up-your-site-with-caching-and-cache-control.html#caching-with-mod_expires

压缩和缓存是不同的东西。

Compression and caching are different things.

对于压缩,PHP Minify或YUI Compressor是伟大的。如果所有的失败,TextMate有一个很好的Javascript工具包,允许你压缩你的代码。您可以手动压缩代码,然后将其上传,并撤消压缩以使源恢复到可读状态。我不建议这样做,但我已经做了。

For compression, PHP Minify or YUI Compressor are great. If all else fails, TextMate has a nice Javascript Tools Bundle that allows your to compress your code. You could manually compress your code, then upload it, and undo the compression to get the source back to a readable state. I do not recommend this, but I have done it before.

对于缓存,请阅读askapache.com网站。有几个简单的方法来打开mod_expires,mod_compress和其他模块。由于你是在共享托管,这里有警告,因为主机可能已关闭某些Apache模块。我使用GoDaddy,他们关闭mod_expires。有关Apache模块的主机特定信息,请参阅主机支持文档。

For caching, read the askapache.com site. There are several easy ways to turn on mod_expires, mod_compress, and other modules. Since you are on shared hosting, here are caveats because the host may have turned off certain Apache modules. I have used GoDaddy, and they turn off mod_expires. For host specific information about Apache modules refer to the hosts support documentation.

如果你幸运,重要的是开启,你可以:

If you are luck, the important ones are on and you can:

<IfModule mod_deflate.c>
  SetOutputFilter DEFLATE
  SetEnvIfNoCase Request_URI \
  \.(?:gif|jpe?g|png)$ no-gzip dont-vary
</IfModule>

<IfModule mod_expires.c>
  ExpiresActive on
  ExpiresByType image/jpg "access 2 month"
  ExpiresByType image/gif "access 2 month"
  ExpiresByType image/jpeg "access 2 month"
  ExpiresByType image/png "access 2 month"
  ExpiresByType text/css "access 2 month"
  ExpiresByType application/x-javascript "access plus 2 month"
  ExpiresByType text/javascript "access plus 2 month"
  ExpiresByType application/javascript "access plus 2 month"
  ExpiresByType image/x-icon "access plus 12 month"
  ExpiresByType image/icon "access plus 12 month"
  ExpiresByType application/x-ico "access plus 12 month"
  ExpiresByType application/ico "access plus 12 month"
</IfModule>`

将此代码放在.htaccess文件中,打开FireFox,然后打开Firebug,然后打开YSlow。运行YSlow测试,它会让你知道缓存是否工作。

Put this code in your .htaccess file, open FireFox, then Firebug, then YSlow. Run the YSlow test, it will let you know whether the caching is working.

这篇关于如何为css / js文件设置缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆