1 Reply Latest reply on Jan 19, 2010 8:11 AM by tanmay.solanki

    Ajax + Browser Caching + Back Button

      When we go to next page and after modifying a page through ajax request, and to the previous version using back button in the browser the browser gives the cached version of the page not without changes of the previous ajax calls on the page.


      1)
      Is there any way using Interceptor so that each time an ajax call is made it synch with the browser cached version?


      2) Alternatively can we disable the caching of the page? so that when browser back button is clicked pages is served from server and not from  the cache?



      I am trying to achieve not caching of page by setting up meta tags in http header on my main page




       <meta http-equiv="Pragma" content="no-cache" />
        <meta http-equiv="Expires" content="-1" />
        <meta http-equiv="CacheControl" content="no-cache" />
        <meta http-equiv="CacheControl" content="no-store" />
        <meta http-equiv="CacheControl" content="must-revalidate" />
        <meta http-equiv="CacheControl" content="private" />
        <meta http-equiv="CacheControl" content="max-stale=0" />
        <meta http-equiv="CacheControl" content="post-check=0" />
        <meta http-equiv="CacheControl" content="pre-check=0" />



      But still the cached version of page is served when I click browser Back Button ?

        • 1. Re: Ajax + Browser Caching + Back Button

          I found solution by using a CustomPhaseListener


          Public class CustomPhaseListener implements PhaseListener {
                    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", "must-revalidate");
                    response.addHeader("Expires", "Mon, 8 Aug 2006 10:00:00 GMT"); // some date in the past
                         
               }
          }