无法通过ExpiresFilter为JavaScript文件添加Expires或Cache-Control响应标头

时间:2017-06-05 06:01:31

标签: tomcat

我在Windows上运行Tomcat 8.0.36上的网站。我按照这个链接

https://tomcat.apache.org/tomcat-8.0-doc/config/filter.html

添加Expires和Cache-Control响应标头。这是我做的:

<filter>
 <filter-name>ExpiresFilter</filter-name>
 <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
 <init-param>
    <param-name>ExpiresByType image</param-name>
    <param-value>access plus 10 minutes</param-value>
 </init-param>
 <init-param>
    <param-name>ExpiresByType text/css</param-name>
    <param-value>access plus 10 minutes</param-value>
 </init-param>
 <init-param>
    <param-name>ExpiresByType application/javascript</param-name>
    <param-value>access plus 10 minutes</param-value>
 </init-param>
</filter>
...
<filter-mapping>
 <filter-name>ExpiresFilter</filter-name>
 <url-pattern>/*</url-pattern>
 <dispatcher>REQUEST</dispatcher>
</filter-mapping>

在web.xml中添加上述内容后,我能够看到css文件的Expires和Cache-Control响应头。但是,我无法看到Javascript文件的此类响应标头。

我做的任何事都错了或丢失了?

1 个答案:

答案 0 :(得分:0)

经过我的测试,似乎javascript的正确初始化代码应为( x-javascript ):

 <init-param>
    <param-name>ExpiresByType application/x-javascript</param-name>
    <param-value>access plus 10 minutes</param-value>
 </init-param>