Javascript 如何为 css/js 文件设置缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2537185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:47:50  来源:igfitidea点击:

how to set cache for css/js file

cssjavascript

提问by coderex

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.

我必须将缓存用于我在站点中使用的 css 文件和 js 文件。我的网站在共享托管服务器中运行。服务器无能为力。

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

那么对于 js 和 css 文件使用缓存和压缩的解决方案是什么。

回答by Christopher Altman

If you are using Apache, I would start here.

如果您使用的是 Apache,我将从这里开始。

Compression and caching are different things.

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

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.

对于压缩,PHP Minify 或 YUI Compressor 都很棒。如果所有其他方法都失败了,TextMate 有一个不错的 Javascript 工具包,可以让您压缩代码。您可以手动压缩您的代码,然后上传它,然后撤消压缩以使源恢复到可读状态。我不推荐这个,但我以前做过。

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.

对于缓存,请阅读 askapache.com 站点。有几种简单的方法可以打开 mod_expires、mod_compress 和其他模块。由于您使用的是共享主机,这里有一些警告,因为主机可能已经关闭了某些 Apache 模块。我用过 GoDaddy,他们关闭了 mod_expires。有关 Apache 模块的主机特定信息,请参阅主机支持文档。

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>`

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.

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

回答by Thiago Belem

If you can use PHP5, use Minify... It will compress, obfuscate and cache your HTML/CSS without changes in the script (JS/CSS) source code.

如果您可以使用 PHP5,请使用Minify...它将压缩、混淆和缓存您的 HTML/CSS,而无需更改脚本 (JS/CSS) 源代码。

If you cannot use PHP5 on your server, compress & obfuscate your files with YUI Compressorand cache your files using .htaccess:

如果您无法在服务器上使用 PHP5,请使用YUI Compressor压缩和混淆您的文件,并使用.htaccess缓存您的文件:

<FilesMatch "\.(css|js)$">
Header set Cache-Control "max-age=172800, public, must-revalidate"
</FilesMatch>

回答by Fitzchak Yitzchaki

You do need an access for the server for manage the http headersthat the server renders to the browser.

您确实需要访问服务器以管理http headers服务器呈现给浏览器的内容。