How to Prevent Browser Caching of JavaScript in Java Backend Apps
This article explains why stale JavaScript can break functionality, outlines the risks of caching such as outdated bugs, security flaws, and debugging difficulties, and provides practical Java‑side solutions—including versioned URLs, cache‑control headers, static‑resource policies, and ETag/Last‑Modified handling—to ensure browsers always load the latest scripts.
1. Why Prevent JavaScript Caching?
Browser caching speeds up page loads but can cause serious issues when JavaScript files are updated. Stale scripts may hide bug fixes, create mismatches between front‑end and back‑end APIs, hinder debugging, and even expose previously patched security vulnerabilities.
2. Practical Solutions
1. Add Version Numbers or Timestamps to URLs
Appending a query parameter forces the browser to treat each update as a new resource.
<script src="app.js?v=1.0.1"></script>Or generate a dynamic timestamp:
<script src="app.js?t=<%= System.currentTimeMillis() %>"></script>2. Set HTTP Response Headers
Configure the server to send cache‑control directives such as Cache-Control, Pragma, and Expires.
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class NoCacheFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");
httpResponse.setHeader("Pragma", "no-cache");
httpResponse.setHeader("Expires", "0");
chain.doFilter(request, response);
}
@Override public void init(FilterConfig filterConfig) throws ServletException {}
@Override public void destroy() {}
}3. Configure Static Resource Cache Policy in Spring Boot
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/js/**")
.addResourceLocations("classpath:/static/js/")
.setCachePeriod(0); // 0 means no caching
}
}4. Use ETag or Last‑Modified Headers
Setting ETag or Last-Modified lets the browser ask the server whether the resource has changed; if not, a 304 response avoids re‑download.
httpResponse.setDateHeader("Last-Modified", System.currentTimeMillis());3. When to Allow Caching?
Cache is beneficial for static assets that rarely change. In such cases, combine caching with versioning or hash‑based filenames to ensure updates are still delivered when needed, balancing performance with freshness.
Java Architecture Stack
Dedicated to original, practical tech insights—from skill advancement to architecture, front‑end to back‑end, the full‑stack path, with Wei Ge guiding you.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
