Cached Resources

  • Authors : Marc Palmer
6 votes

13% of Grails users

Dependency :
runtime ":cached-resources:1.0"

Documentation Source Issues

Summary

Provides a "hash and cache" mapper for the resources framework, automatically creating safe unique filenames for your resources and setting them to eternally cache in the browser.

Description

This plugin leverages the Resources plugin to make static resources browser-cacheable with unique filenames based on their content. It works well together with zipped-resources plugin

There is no application versioning required as this plugin uses a similar approach to "git". It hashes files and renames them to their hash. So when your resource file contents change, the file names automatically change - no more problems with stale JS or CSS content in your client browsers.

The result is that even if your application version changes between deployments, if some resources did not change in that release, all your users will still be able to continue using their previous long-term cached file without reloading from your site.

It is zero-hassle static file caching.

Usage

Just install:

grails install-plugin cached-resources

See the documentation for the resources plugin for how to define your resources.

Commercial Support

Commercial support is available for this and other Grailsrocks plugins.

Resource mappers

This plugin adds the resource mapper called hashandcache which you can include/exclude based on URIs or individual resources using normal Resources configuration mechanisms.

Configuration

There are mapper-specific Config values supported:

  • flatten - Controls whether or not the mapper should flatten directories after creating the unique file names
  • shorten - Controls whether or not the mapper should shorten the generated hashed file names using base62 encoding (yes that's 62 not 64).
You set these using standard per-mapper config mechanisms supplied by Resources.

Details

Each file is hashes using SHA-256. Using the resources plugin, it is able to rename the file and change the URL generated when your application links to the resource.

Then, when requests come in, it adds all the required caching headers to attempt "eternal" caching by intermediate caches and the client browser. It does this using the cache-headers plugin.

Configuration

You may want to control which file types are hashed and cached, or exclude all hashing and caching in the development environment. For details see the configuration documentation for the Resources plugin.