正确设置HTTP头以强制JS,CSS和PNG文件缓存 [英] Set HTTP headers properly to force caching on JS, CSS and PNG files

查看:262
本文介绍了正确设置HTTP头以强制JS,CSS和PNG文件缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何告诉GlassFish服务器,将所有JS,CSS和PNG文件存储到浏览器缓存中以减少HTTP GET请求?

我正在使用JSF和PrimeFaces。

解决方案

只需使用JSF内置资源处理程序。即使用 < ; h:outputStylesheet name> < h:outputScript name> < h:graphicImage name> / resources 文件夹中的文件而不是普通香草< link rel =stylesheet> < script> < img>

 < h:outputStylesheet name =css / style.css/> 
< h:outputScript name =js / script.js/>

这样您根本不用担心资源缓存。 JSF内置资源处理程序已经设置了必要的响应标头。到期时间默认为1周。



在Mojarra,您可以通过以下上下文参数控制到期时间(值以毫秒为单位):

< context-param>
< param-name> com.sun.faces.defaultResourceMaxAge< / param-name>
<参数值> 3628800000< /参数值> <! - 6周。 - >
< / context-param>

在MyFaces中:

 < context-param> 
< param-name> org.apache.myfaces.RESOURCE_MAX_TIME_EXPIRES< / param-name>
<参数值> 3628800000< /参数值> <! - 6周。 - >
< / context-param>



另请参阅:




How can I tell to GlassFish server, to store all JS, CSS and PNG files into browser cache in order to reduce HTTP GET requests?

I am using JSF and PrimeFaces.

解决方案

Just make use of JSF builtin resource handler. I.e. use <h:outputStylesheet name>, <h:outputScript name> and <h:graphicImage name> with files in /resources folder instead of "plain vanilla" <link rel="stylesheet">, <script> and <img>.

<h:outputStylesheet name="css/style.css" />
<h:outputScript name="js/script.js" />
<h:graphicImage name="images/logo.png" />

This way you don't need to worry about resource caching at all. JSF builtin resource handler has already set the necessary response headers. The expiration time defaults already to 1 week.

In Mojarra you can control the expiration time by the following context parameter (the value is in millis):

<context-param>
    <param-name>com.sun.faces.defaultResourceMaxAge</param-name>
    <param-value>3628800000</param-value> <!-- 6 weeks. -->
</context-param>

And in MyFaces:

<context-param>
    <param-name>org.apache.myfaces.RESOURCE_MAX_TIME_EXPIRES</param-name>
    <param-value>3628800000</param-value> <!-- 6 weeks. -->
</context-param>

See also:

这篇关于正确设置HTTP头以强制JS,CSS和PNG文件缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆