正确设置HTTP头以强制JS,CSS和PNG文件缓存 [英] Set HTTP headers properly to force caching on JS, CSS and PNG files
问题描述
如何告诉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:
- How to reference CSS / JS / image resource in Facelets template?
- What is the JSF resource library for and how should it be used?
这篇关于正确设置HTTP头以强制JS,CSS和PNG文件缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!