Set cache headers on an XPage

var exCon = facesContext.getExternalContext(); 
var writer = facesContext.getResponseWriter(); 
var response = exCon.getResponse(); 

//set the expires header to 30 days after now 
var now = new Date(); 
response.setDateHeader("Expires", now.getTime() + (30*24*60*60*1000)); 
response.setHeader("Cache-Control", "public"); 

//sample: send some JavaScript to the browser
response.setHeader("Content-Type", "application/x-javascript"); 
writer.write( "var foo = \"bar\";" ); 

writer.endDocument(); 
facesContext.responseComplete();





The code shows how to set a far future expires header and the cache-control directive. With these header the browser will cache the resource and serve it from its cache until the expiration date (without a server hit to check if the resource has changed).

Note that the sample is used to output a piece of (client side) JavaScript.

XPages
Mark Leusink
November 20, 2011 4:38 AM
Rating
338

All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.



No comments yetLogin first to comment...