6

I am using jboss Server 7.1.1 for deploying a java web application. For js, css, font files the browser cache is not getting updated with new changes. Every time we insist the clients to clear cache in their browsers to get the new changes to effect.

is there any configuration which i can configure, so that when a new code is deployed i can guide all the request to be provided with the updated file ?

by googling on this topic i find that, we can write custom methods to set e-tag value. is there any configuration which can help me doing it?

most of the time we do a hot deploy (upload the war file in jboss management console with out down time). so i doubt whether the custom e-tag will be updated or not during hot deployment as we configure it on server startup.

or is there a way to handle it in web application's web.xml file ?

I need a solution which can use browser cache till next deployment happens for js, css and font files. i don't what to set "expires" header. because we don't have fixed deployment cycles.

Let me know if you need more info to arrive at a solution.

siva
  • 516
  • 3
  • 7
  • 22

3 Answers3

3

Since you are using jboss for deploying your application, I guess that your pages are not static.

A simple solution if you don't want to mess with Cache-Control, would be to append a dynamic "cache bust" next to each resource url. So your resource urls (e.g. inside a .jsp) would be something like this:

<link rel="stylesheet" href="styles.css?_=${cacheBust}">
<script type="text/javascript" src="script.js?_=${cacheBust}">

Now you need a place to generate a new cacheBust value on each deployment/redeployment. This could be:

an HttpServlet loaded on startup:

@WebServlet(loadOnStartup = 1) 
public class InitServlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
        super.init();
        getServletContext().setAttribute("cacheBust", UUID.randomUUID().toString());
    }

}

or a ServletContextListener

@WebListener
public class InitListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        sce.getServletContext().setAttribute("cacheBust", UUID.randomUUID().toString());
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    }

}
Marinos An
  • 9,481
  • 6
  • 63
  • 96
1

You want browser caching of rarely changed files. But sometimes they do change and then you want to trigger browser cache clear. Something like:

<script type="text/javascript">
localStorage.clear();
</script>

However the reliable solution is to use versions on the server side:

<link type="text/css" rel="stylesheet" href="/css/mystyles-1.01.css">
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

According to this post you can disable caching by changing the response header. You can do this using response.setHeader("Cache-Control", "no-cache, no-store");

you can also add a query string to the link (see this post) For example you can use ./design.css?v=1 initially while changing the references to ./design.css?v=1 the next time you update design.css. You can dynamically change the numbers

dan1st
  • 12,568
  • 8
  • 34
  • 67
  • i actually don't what to disable cache. i want the browser to update cache if there is a change in file(js, css, font, etc). This can be done by configuring e-tags header (Am not 100% sure if this will works). in case, if that works, i want to know how to configure it properly. else i expect an answer that explains "what else can help here?" and "why e-tags cant help here?". – siva Apr 29 '19 at 08:58
  • you can also add a query string to the link (see [this post](https://stackoverflow.com/questions/1922910/force-browser-to-clear-cache).) For example you can do _./design.css?v=1_ and the first time you can use _./design.css?v=1_ the next time. – dan1st Apr 29 '19 at 09:38