I have Spring-boot application with React frontend. All is bundled together to a single *.jar file with gradle.
My production bundle of my React frontend is located in src/main/resources/static folder. In production *.jar file is located in BOOT-INF/classes/static folder. In this static folder is located index.html and another static folder with media, js and css folders.
The problem I am trying to solve is, that browsers are caching the main index.html file. in this index, all hashes of bundles are correct, but the index itself is not always requested from the server, but from the browser cache.
I've tried the following things to prevent caching:
@Configuration
public class BaseConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/")
.addResourceLocations("classpath:/static/index.html")
.setCacheControl(CacheControl.maxAge(0, TimeUnit.SECONDS).mustRevalidate())
.setCacheControl(CacheControl.noCache())
.setCacheControl(CacheControl.noStore())
.resourceChain(true)
.addResolver(new PathResourceResolver());
}
}
and this in one of my controllers:
@GetMapping("/index.html")
public void getIndex(HttpServletResponse response) {
response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, max-age=0, must-revalidate");
}
But without any luck. This is my response headers from the server:
I assume that every time I press the f5 key (refresh the page), a new index.html with a new hashes should be loaded from the server. But that is not happening. Even when the application is not running on the server, the old index.html still returns from the browser, in which the whole React app is started.
How can I force the browsers to load index.html every time when I press the f5, or when I open the new browser tab?
Thanks for any answer.
It was my mistake, the problem was a registered React ServiceWorker, not the bad Spring-boot configuration.