Html 设置缓存过期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7134425/
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-29 10:10:33  来源:igfitidea点击:

Set cache expiration?

htmlcaching

提问by lisovaccaro

I tested my site with Chrome and got the following recommendation:

我用 Chrome 测试了我的网站并得到以下建议:

The following resources are missing a cache expiration. Resources that do not specify an expiration may not be cached by browsers:
style.css
jquery.marquee.js
jquery.marquee.css
logo.png

How do I set the cache expiration for these files?

如何为这些文件设置缓存过期时间?

采纳答案by Matt Ball

Following the Yahoo! Best Practices for Speeding Up Your Web Site,you should Add an Expires or a Cache-Control Headerand Configure ETags.

雅虎!加速您的网站的最佳实践,您应该添加 Expires 或 Cache-Control 标头配置 ETags

How you actually go about configuring the server to do this depends on far more information than you have provided in the question.

您实际如何配置服务器以执行此操作取决于比您在问题中提供的信息多得多的信息。

回答by Shankar Prakash G

One of the way to Set cache expiration is by using .htaccess file.

设置缓存过期的方法之一是使用 .htaccess 文件。

Below code will set expiration for it's respective file type, e.g. for CSS files expiration will be 14 days.

下面的代码将为其各自的文件类型设置过期时间,例如 CSS 文件的过期时间为 14 天。

<IfModule mod_expires.c>
   ExpiresActive on
   ExpiresDefault "access plus 1 month"
   ExpiresByType application/javascript "access plus 1 year"
   ExpiresByType image/x-ico "access plus 1 year"
   ExpiresByType image/jpg "access plus 14 days"
   ExpiresByType image/jpeg "access plus 14 days"
   ExpiresByType image/gif "access plus 14 days"
   ExpiresByType image/png "access plus 14 days"
   ExpiresByType text/css "access plus 14 days"
</IfModule>

回答by tushar bhatia

If this question pertains to caching of javascript or css that are a part of your tomcat application directory; you may like to examine the /conf/web.xml file.

如果此问题与作为 tomcat 应用程序目录一部分的 javascript 或 css 的缓存有关;您可能想检查 /conf/web.xml 文件。

Typically the mime mapping available here is
<mime-mapping>
   <extension>js</extension>
   <mime-type>application/javascript</mime-type>
</mime-mapping>

通常这里可用的 mime 映射是
<mime-mapping>
   <extension>js</extension>
   <mime-type>application/javascript</mime-type>
</mime-mapping>

In apache the default directive is
   ExpiresByType text/javascript "access plus <specify your timeframe>"

在 apache 中,默认指令是
   ExpiresByType text/javascript "access plus <specify your timeframe>"

You may like to change the apache directive to application/javascript or change the tomcat mime mapping to text/javascript and this will set the expiration to your timeframe.

您可能希望将 apache 指令更改为 application/javascript 或将 tomcat mime 映射更改为 text/javascript,这会将到期时间设置为您的时间范围。