Tuesday, June 26, 2012

Re: Perfect caching problem

I send the exact same headers - though I let Tomcat's default servlet handle the "Date" one, and I just hardcode the Expires header to "Thu, 01 Jan 1970 00:00:00 GMT".  I am running on Tomcat 6 and not Glassfish, but I have not encountered the issue you're seeing.

This may help, here's my server's response headers for a GET request to our nocache file (Tomcat is handling everything aside from the Cache-Control, Expires and Pragma headers):

HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Content-Length: 7263
Content-Type: text/javascript
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Last-Modified: Mon, 25 Jun 2012 11:07:44 GMT
Accept-Ranges: bytes
Etag: W/"7263-1340622464989"
Date: Tue, 26 Jun 2012 14:29:40 GMT


On Monday, June 25, 2012 11:27:10 AM UTC-4, Jonas wrote:
I am trying to implement the GWT "perfect caching" by using a custom javax servlet filter. All files containing .nocache. should never be cached, and files containing .cache. should be cached for a week.

Here is the code for my filter:

public class GWTCacheControlFilter implements Filter
{
    @Override
    public void destroy()
    {   
    }
   
    @Override
    public void init(final FilterConfig config) throws ServletException
    {       
    }
   
    @Override
    public final void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain filterChain)
        throws IOException, ServletException
    {
        final HttpServletRequest httpRequest = (HttpServletRequest) request;
        final String requestURI = httpRequest.getRequestURI();
       
        if (requestURI.contains(".nocache."))
        {
            final Date now = new Date();
            final HttpServletResponse httpResponse = (HttpServletResponse) response;
            httpResponse.setDateHeader("Date", now.getTime());
            httpResponse.setDateHeader("Expires", now.getTime() - 86400000L);
           
            httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0
            httpResponse.setHeader("Cache-control", "no-cache, no-store, must-revalidate"); // HTTP 1.1                       
        }
        else if (requestURI.contains(".cache."))
        {           
            final HttpServletResponse httpResponse = (HttpServletResponse) response;
            httpResponse.setDateHeader("Expires", System.currentTimeMillis() + 604800000L); // 1 week in future.
        }
       
        filterChain.doFilter(request, response);
    }
}

When I reload the page I see in Firebug a 304 Not Modified status on my initial myapp.nocache.js file, which sometimes leads to people seeing only a white page after a new deploy.  If I inspect the headers of the nocache file I don't see any Expires-tag.

The .cache. files seem to be cached correctly however (one week):

Date: Mon, 25 Jun 2012 15:11:57 GMT
Expires: Mon, 02 Jul 2012 15:11:57 GMT

I am using guice so in my servlet module I do:

filter("*").through(GWTCacheControlFilter.class);

However I have also tried adding the following to web.xml:

    <filter>
         <filter-name>gwtCacheControlFilter</filter-name>
         <filter-class>my.package.GWTCacheControlFilter</filter-class>
    </filter>

    <filter-mapping>
         <filter-name>gwtCacheControlFilter</filter-name>
         <url-pattern>/*</url-pattern>
    </filter-mapping>

Anyone have some ideas what I am missing? I'm deploying using Glassfish Open Source edition 3.1.1.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/bDWi2rPVpMQJ.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment