Caching with Play framework and Java
Asked Answered
D

1

8

I am running an application with Play and Java, and I need to set up expiration date for various types of assets: images, css, javascript etc.

I have the following in the conf/routes file:

GET     /assets/*file               controllers.Assets.at(path="/public", file)

I was able to set expiration date for one individual file in application.conf:

"assets.cache./public/js/pages/validation.js"="max-age=7200"

But I am not able to set it for a whole folder. I have tried

"assets.cache./public/js/pages/*.js"="max-age=7200"
"assets.cache./public/js/pages/*"="max-age=7200"

but nothing happens. I was hoping to set the expiration date for everything in the /js/pages folder.

I've also tried

assets.defaultCache="max-age=7200"

per instructions at http://www.jamesward.com/2014/04/29/optimizing-static-asset-loading-with-play-framework

as well as

http.cacheControl=7200

per documentation http://www.playframework.com/documentation/1.2.3/configuration#http

and none of these work. The changes above were done in application.conf.

I know there is a way to do the same by defining controllers that change the response() for the routes that I want to set the expiration date for: far future Expires header for static contents

But I would like to know how to configure expiration date for assets from the application.conf file.

Our application is running on S3 Linux instances, so configuring the expire date on the server is not an option.

Thank you!

Delogu answered 3/6, 2014 at 16:57 Comment(0)
E
4

Play framework does not support "assets.cache./public/js/pages/*.js"="max-age=7200" but assets.defaultCache="max-age=7200" should work.

In debug/dev mode (starting app using play run) assets.defaultCache is ignored, so it is always 'no-cache'. Make sure you are running it in prod mode(using play start).

I can't find any reference in docs, but same can be checked in https://github.com/playframework/playframework/blob/master/framework/src/play/src/main/scala/play/api/controllers/Assets.scala AssetInfo::cacheControl function

Entwine answered 19/12, 2014 at 8:36 Comment(1)
Could you please reference the specific documentation of Play2 in your answerSelfreliance

© 2022 - 2024 — McMap. All rights reserved.