Set cache expiration?

2019-01-10 22:57发布

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

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?

标签: html caching
3条回答
放荡不羁爱自由
2楼-- · 2019-01-10 23:22

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.

Typically the mime mapping available here is
<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>"

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.

查看更多
神经病院院长
3楼-- · 2019-01-10 23:23

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

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

<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>
查看更多
三岁会撩人
4楼-- · 2019-01-10 23:42

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

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

查看更多
登录 后发表回答