How to make the browser to cache the static resources?

UI Components for JSF
Post Reply
User avatar
MISS_DUKE
Posts: 273
Joined: 08 Aug 2010, 05:52

03 Dec 2010, 12:27

I am trying to force the browser to cache the static resources like Javascript and image files. I try to use a Servlet filter to achieve this by setting "Expires" response header when one of these resources is requested.

But, the response headers set by my Servlet filter is simply ignored and it seems the server is setting a default value for "Expires" response header.

When I test in my local machine the "Expires" header is set to 7 days ahead, but my production server sets this as only 10 minutes ahead!

How can I set the "Expires" header to my desired value?
JSF implementation: Mojarra V2.1.7
JSF component library: Primefaces V3.1.1
Server: GlassFish Open Source Edition V3.1.1 (build 12)

voyagerx
Posts: 70
Joined: 10 Feb 2011, 03:52

15 Feb 2011, 23:48

Is there any chance somebody has figured this out?

My Expires header is actually set to the same second as the response causing there to be no caching at all.

This is causing the jQuery script to be loaded on every page request. I enabled compression in Glassfish which took the script size from 200K to 50K, but still, there has to be a way to set the Expires header.

voyagerx
Posts: 70
Joined: 10 Feb 2011, 03:52

16 Feb 2011, 00:10

I found the answer to my own question.

If javax.faces.PROJECT_STAGE in your web.xml is set to Development, it automatically sets it to not cache (expires=current time).

If it is set you Production, it sets it to 10 mins.

You can override the 10 mins by adding another context paremeter: com.sun.faces.defaultResourceMaxAge

The default of this is 604800 which is ~10mins.

Hope this helps somebody else. No servlet filters needed.

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

16 Feb 2011, 00:22

Thanks for sharing.

User avatar
MISS_DUKE
Posts: 273
Joined: 08 Aug 2010, 05:52

16 Feb 2011, 11:36

@voyagerx
Really useful information. I was not luck enough to find this when Googling about JSF caching.
JSF implementation: Mojarra V2.1.7
JSF component library: Primefaces V3.1.1
Server: GlassFish Open Source Edition V3.1.1 (build 12)

voyagerx
Posts: 70
Joined: 10 Feb 2011, 03:52

20 Feb 2011, 12:40

No problem. Hope it helped. Took me a while on google as well.

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

20 Feb 2011, 12:48

probably the best way is to implement an own servletfilter for caching
Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

20 Feb 2011, 13:03

http://ovaraksin.blogspot.com/2011/02/i ... ustom.html

Might help as well for invalidating when you update, I haven't used it though.

User avatar
Oleg
Expert Member
Posts: 3805
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

20 Feb 2011, 13:40

Thanks Cagatay,

I posted what we implemented recently in our web app and it's working of course.
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 36 guests