4

How can I tell to GlassFish server, to store all JS, CSS and PNG files into browser cache in order to reduce HTTP GET requests?

I am using JSF and PrimeFaces.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user1838597
  • 53
  • 1
  • 5

1 Answers1

7

Just make use of JSF builtin resource handler. I.e. use <h:outputStylesheet name>, <h:outputScript name> and <h:graphicImage name> with files in /resources folder instead of "plain vanilla" <link rel="stylesheet">, <script> and <img>.

<h:outputStylesheet name="css/style.css" />
<h:outputScript name="js/script.js" />
<h:graphicImage name="images/logo.png" />

This way you don't need to worry about resource caching at all. JSF builtin resource handler has already set the necessary response headers. The expiration time defaults already to 1 week.

In Mojarra you can control the expiration time by the following context parameter (the value is in millis):

<context-param>
    <param-name>com.sun.faces.defaultResourceMaxAge</param-name>
    <param-value>3628800000</param-value> <!-- 6 weeks. -->
</context-param>

And in MyFaces:

<context-param>
    <param-name>org.apache.myfaces.RESOURCE_MAX_TIME_EXPIRES</param-name>
    <param-value>3628800000</param-value> <!-- 6 weeks. -->
</context-param>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you for the answer.. But looking at the HTTP server monitor on netbeans, i noticed the file that takes more time to download is primefaces.js , and i never call that script in my pages... – user1838597 Aug 23 '13 at 00:52
  • PrimeFaces components do. – BalusC Aug 23 '13 at 00:56
  • Yes, primefaces components, generates HTML+js code , and averytime i refresh the page, a GET request is called to download primefaces.js , jquery.js and so on... This files are not in /resources folder so how can i tell "store those files in cache memory" ? – user1838597 Aug 23 '13 at 01:29
  • Those **are** JSF resource dependencies. They are bundled in PrimeFaces JAR file (for detail, see also http://stackoverflow.com/q/8320486/). Look closer at the HTTP traffic. If you don't refresh but just navigate to another page which also needs that file, then browser won't request it at all. If you refresh the page, then the browser will indeed send a GET request, but the server will return a 304 response without body. Only if you perform a "hard refresh" then it'll be a 200 response and contain the file in the body. – BalusC Aug 23 '13 at 02:19
  • & how do you force caching on images referenced in css files? there we can't use things like ``.. – Rajat Gupta Sep 18 '13 at 12:23
  • @user01: They will be if you use `#{resource}`. – BalusC Sep 18 '13 at 12:24
  • any other ways? I don't really like putting EL in css or js files. – Rajat Gupta Sep 18 '13 at 12:51
  • also I think that increases *some* work for JSF if I have considerably large no of images in css – Rajat Gupta Sep 18 '13 at 12:54
  • @user01: consider OmniFaces `UnmappedResourceHandler`. Otherwise, bite the bullet, fix them all and learn the lesson for next JSF project. Hint: Eclipse has builtin regexp find&replace. – BalusC Sep 18 '13 at 13:05
  • noobs (like me) should also be aware that having the `javax.faces.PROJECT_STAGE` context-param set to "Development" (in `web.xml`), _seems_ to disable caching. – frIT Mar 20 '15 at 11:02