It happened to me that my Wicket web application does not reliably reload static resources such as CSS stylesheets or JavaScript code. The problem need not be Wicket in this respect, but may also result from the browser’s caching policy. Wicket can circumvent this problem by updating the lastModified property of static resources.

Wicket 1.5 and above

Wicket 1.5 has introduced an advanced caching configuration, that allows you to even implement your own caching strategies (see here). Basically, you now have to create an instance of IResourceCachingStrategy that uses a particular IResourceVersion. The IResourceCachingStrategy determines how the filename is modified and the IResourceVersion determines how the current version information is calculated (e.g. last modified, checksum,…). A minimal working example looks like this:

import org.apache.wicket.request.resource.caching.FilenameWithVersionResourceCachingStrategy;
import org.apache.wicket.request.resource.caching.version.LastModifiedResourceVersion;

public class MyApplication extends WebApplication {
    @Override
    protected void init() {
        IResourceCachingStrategy strategy = new FilenameWithVersionResourceCachingStrategy(
                new LastModifiedResourceVersion());
        this.getResourceSettings().setCachingStrategy(strategy);
    }
}

The FilenameWithVersionResourceCachingStrategy is the suggested strategy. The following implementations of IResourceVersion are available:

  • LastModifiedResourceVersion uses the last modified timestamp.
  • CachingResourceVersion limits the liftetime of a cached resource to the lifetime of the object and an configured cache size.
  • MessageDigestResourceVersion calculates a hash of the cached resource. It can use various algorithms from the Java Cryptographic Architecture, by default MD5.
  • RequestCycleCachedResourceVersion caches the resources over one HTTP request lifecycle.
  • StaticResourceVersion uses a static resource version.

Before Wicket 1.5

Before Wicket 1.5, it was fairly easy: You simply needed to set a flag in the application’s settings:

public class MyApplication extends WebApplication {
    @Override
    protected void init() {
        getResourceSettings().setAddLastModifiedTimeToResourceReferenceUrl(true);
    }
}

Resources

  • [1] Solution for Wicket < 1.5
  • [2] Notes on the changes introduced in Wicket 1.5
  • [3] Javadoc of FilenameWithVersionResourceCachingStrategy