Saurav Shah
Saurav Shah

Reputation: 681

PlayFramework 2.0.1 asset versioning

I like the support of LESS css and Google Closure compiler for Play 2. Is there a way to add support for automatic versioning as well so that I don't have to worry about renaming files everytime a make a change. How do other folks in the community handle this?

Upvotes: 1

Views: 850

Answers (1)

kheraud
kheraud

Reputation: 5288

I assume this is a caching problem you are talking about. If it is a versionning problem you are talking about, this is the work of a Version Control System like git or mercurial.

You will have this caching problem with every static resources.

The problem

In order to have web pages displayed quickly on the client side (and in order to have not so many requests for static contents on the server side) you will add Cache-Control and Etag directives in you HTTP responses. The browser will then get these static resources from its cache.

But if you change your logo.png (changing you firm logo for example), as this image is stored in the browser cache, the users won't see you new logo.

Solutions

I see two solutions for that:

  • Play with the Etag and Cache-control directives to let the user request the new static resources every X days. I do that with a front-end server (Nginx for example) but you can also use play support for that (detailed here)
  • Create a fake query parameters which contains the resource version. For example you can have http://www.mydomain.com/public/images/logo.png?v=1.2. If you change the v parameter, the browser won't retrieve it from cache. On the server side the query parameter won't change anything and the served resource will be cached again.

I use the second solution with a configuration parameter in my application.conf appended to all static resources. This assumes that when you update a single resource, all the resources are reloaded again (generaly, when you change your images, you will also change your css and, why not, your js files...)

Upvotes: 1

Related Questions