问题
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.
回答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">
回答2:
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) {
}
}
回答3:
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");
[EDIT]
you can also add a query string to the link (see this post) For example you can do ./design.css?v=1 and the first time you can use ./design.css?v=1 the next time. You can dynamically change the numbers
来源:https://stackoverflow.com/questions/55808264/browser-cache-files-not-getting-updated-after-a-new-deployment-in-jboss-server-f