Search code examples
jsfprimefacesmojarra

Resources first checked against server not retrieved from cache right away


Resources that are in ui:insert's,ui:include's are not cached. While the other stuff that is defined in my template is. The resources that are accessed in the ui:'s do have the right "Expires"-header, but they also have Cache-Control:no-cache & Pragma:No-cache. The other resources that are not accessed in the ui:insert and ui:include don't have this, like the stylesheets and js in the header. Now and then some random resources in the ui:'s do get from the cache, like 2 out of 12. And those 12 images are defined exactly the same way, I don't get why those 2 would get cached.

I have tried everything I can, and can't figure it out. (Mojarra 2.2.12 is used)

The param javax.faces.PROJECT_STAGE has the value Production and the param com.sun.faces.defaultResourceMaxAge has 604800000.

Example of my main_pages_template, is use this template at various pages with the template-attribute in the ui:composition.

<html lang="#{localeBean.language}"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://xmlns.jcp.org/jsf/facelets"      
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:view locale="#{localeBean.language}">
    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <h:outputStylesheet library="css" name="default.css"/>
        <h:outputStylesheet library="css" name="cssLayout.css"/>
        <h:outputScript library="js" name="locales.js"/>
        <title><ui:insert name="title">Facelets Template</ui:insert></title>

        <ui:include src="/WEB-INF/templates/favicon.xhtml" />

    </h:head>

    <h:body id="secure">

        <div id="wrapper">
            <div id="top">
                <ui:include src="/WEB-INF/templates/header_template.xhtml" />
            </div>

            <div id="content" class="center_content">            
                <ui:insert name="content">Content</ui:insert>
            </div>

            <div id="footer">            
                <ui:include src="/WEB-INF/templates/footer_template.xhtml" />
            </div>
        </div>

    </h:body>
</f:view>

Edit: Expample of the headers of an image that is not retrieved from the cache.

Response headers:

Cache-Control:no-cache
Content-Length:6607
Content-Type:image/png
Date:Fri, 15 Jan 2016 15:55:26 GMT
ETag:W/"6607-1452873128742"
Expires:Sun, 14 Feb 2016 15:55:26 GMT
Last-Modified:Fri, 15 Jan 2016 15:52:08 GMT
Pragma:No-cache
Server:GlassFish Server Open Source Edition  4.0
X-Powered-By:Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition  4.0  Java/Oracle Corporation/1.8)

Request headers:

Accept:image/webp,image/*,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,nl;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Cookie:JSESSIONID=5ff7d3adafb1d4c47c82149f04c8; JSESSIONID=a56f8de3d3ad495ef464b309b91a; treeForm_tree-hi=treeForm:tree:resources:mailResources
Host:localhost:8080
If-Modified-Since:Fri, 15 Jan 2016 15:52:08 GMT
If-None-Match:W/"6607-1452873128742"
Referer:http://localhost:8080/webapp/secure/profile
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36

Solution

  • I figured out that the server was responding with Cache-Control: no-cache and Pragma: no-cache because the resources were accessed on a secure page that was defined by a GlassFish Security Realm (j_security_check).

    To solve this I implementend the solution here: Static resources are not cached referenced from glassfish secure page

    I added <property name="securePagesWithPragma" value="false" /> as a property from glassfish-web-app in the glassfish-web.xml. Now only the Cache-Control header is set with the value, private which is fine. Private means that only the end user can cache it and not inbetween proxies.