3

In my Spring JSF facelets web application in Jboss6 server I need to make sure certain web pages do not get cached by the web browser for security reasons. And it should work as cross browser as well. I found out when I ran the test in YSlow plugin in firefox the below recommendations.

enter image description here

In my web application I have a Phaselistener set to disable cache as well. But I still couldn't figure why these results are given by YSlow and I want to know how to resolve this issue by setting the far future expiration date to those static components and improve the performance of this page as well.

cache control phaselistener

public class CacheControlPhaseListener implements PhaseListener {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public PhaseId getPhaseId() {
        return PhaseId.RENDER_RESPONSE;
    }

    public void afterPhase(PhaseEvent event) {
    }

    public void beforePhase(PhaseEvent event) {
        FacesContext facesContext = event.getFacesContext();
        HttpServletResponse response = (HttpServletResponse) facesContext
                .getExternalContext().getResponse();
        response.addHeader("Pragma", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
        response.addHeader("Cache-Control", "no-store");
        response.addHeader("Cache-Control", "must-revalidate");
        response.addHeader("Expires", "Mon, 8 Aug 2006 10:00:00 GMT");
    }
}

In faceconfig

<lifecycle>
    <phase-listener id="nocache">com.company.jsf.listener.CacheControlPhaseListener</phase-listener>
</lifecycle>

the http header

enter image description here

After adding future dates YSlow still display the following,

enter image description here

Swarne27
  • 5,521
  • 7
  • 26
  • 41
  • use filter instead... http://stackoverflow.com/a/8185628/617373 / http://stackoverflow.com/a/10657913/617373 / http://stackoverflow.com/a/10854623/617373 – Daniel Dec 19 '12 at 07:34
  • i tried the filter, but still the same – Swarne27 Dec 19 '12 at 09:13
  • i want to know how to set the far future expiration date to those static components. Im using jboss server. – Swarne27 Dec 19 '12 at 11:58

1 Answers1

4
response.addHeader("Expires", "Mon, 8 Aug 2006 10:00:00 GMT");

8 august 2006 is as of today definitely not a "far future" expiration date.

You need to set a real far future expiration date, e.g. 30 days after today.

response.setDateHeader("Expires", System.currentTimeMillis() + (30 * 24 * 60 * 60 * 1000L));

See also:


Unrelated to the concrete problem, your cache control and pragma header tells that those resources should be not cached at all. This makes no sense. Remove them. Also, using a phase listener approach instead of a filter approach is clumsy.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the explanation, I modified my phaselistener accordingly but I still get YSlow displaying three urls. Suppose I cant add the image to the comment box I've added it to the question. pls check and give me some advice Thanks @BalusC +1 for your help so far – Swarne27 Dec 20 '12 at 06:25
  • Those files are not been served through `FacesServlet`, but through container's default servlet. A `PhaseListener` kicks only in on JSF requests (and that's one of the main reasons why it's clumsy). – BalusC Dec 20 '12 at 10:25
  • So there's no solution for that then? will a filter be of any help? – Swarne27 Dec 20 '12 at 10:31
  • Either change their URLs accordingly so that they go through the `FacesServlet`, so that any `PhaseListener` will run, or change the `PhaseListener` to be a `Filter` so that it runs regardless of if it's a JSF request or not. – BalusC Dec 20 '12 at 10:32
  • It still gives "(no expires) https://localhost:8443/favicon.ico". But the problem is I actually don't have a "favicon.ico" in my application as well. How does this point to something which is not there. Damn! – Swarne27 Dec 20 '12 at 11:31
  • It's browser-specific request (for the icon left from the URL in address bar). It might actually have returned a 404. No panic, just create one yourself if necessary: http://tools.dynamicdrive.com/favicon/ – BalusC Dec 20 '12 at 14:56
  • Thanks @BalusC you saved lot of my time.By the way i had to add 1000L to "response.setDateHeader("Expires", System.currentTimeMillis() + (30 * 24 * 60 * 60 * 1000L));" because the calculation gives 1 month back, it the data type long issue. – Swarne27 Dec 21 '12 at 07:55