我测试我的网站与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
如何设置这些文件的缓存过期?
我测试我的网站与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
如何设置这些文件的缓存过期?
继Yahoo! Best Practices for Speeding Up Your Web Site, 您应该Add an Expires or a Cache-Control Header和Configure ETags。
实际上,如何配置服务器来完成此操作取决于您提供的信息远多于您在问题中提供的信息。
如果此问题属于缓存JavaScript或css,它们是您的tomcat应用程序目录的一部分;您可能想要检查/conf/web.xml文件。
通常可在这里的mime映射是
< MIME映射>
<延伸> JS < /延伸>
< mime类型>应用/ JavaScript的</MIME类型>
</mime-mapping >
在apache的默认指令是
ExpiresByType文/ JavaScript的“访问加<指定的时间内>”
你可能喜欢的Apache指令更改为应用程序/ JavaScript或更改Tomcat MIME映射到文本/ JavaScript,这将设置过期到你的时间表。
设置缓存过期的一种方法是使用.htaccess文件。
下面的代码将为其各自的文件类型设置过期时间,例如,对于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>
有没有一种方法可以在需要时清除缓存?(例如更新后)? –
@ Reign.85:您无法强制浏览器清除缓存。您可以使用'style.css'等文件对'style.css?v1.1'进行版本控制,或者重命名文件将解决问题。 –
如果我从.htaccess中删除了该代码,那么浏览器将获得最新版本,而无需重命名文件(或使用版本控制)? –
你在运行什么服务器? –