Skip to content

Commit 136046f

Browse files
lcompleteclaude
andcommitted
Optimize static resource caching with custom configuration
Refactored web resource caching to use a custom WebResourceCacheConfig for better control over cache headers. Changed ReactAppController to use forward instead of direct path to ensure proper cache handling. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent a209d56 commit 136046f

3 files changed

Lines changed: 51 additions & 6 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.huntly.server.config;
2+
3+
import java.util.concurrent.TimeUnit;
4+
import org.springframework.boot.autoconfigure.web.WebProperties;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.http.CacheControl;
7+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
8+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
9+
10+
@Configuration
11+
public class WebResourceCacheConfig implements WebMvcConfigurer {
12+
13+
private final WebProperties.Resources resources;
14+
15+
public WebResourceCacheConfig(WebProperties webProperties) {
16+
this.resources = webProperties.getResources();
17+
}
18+
19+
@Override
20+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
21+
String[] staticLocations = resources.getStaticLocations();
22+
CacheControl noCache = CacheControl.noCache();
23+
CacheControl weekCache = CacheControl.maxAge(7, TimeUnit.DAYS).cachePublic();
24+
CacheControl dayCache = CacheControl.maxAge(1, TimeUnit.DAYS).cachePublic();
25+
26+
registry.addResourceHandler("/index.html")
27+
.addResourceLocations(staticLocations)
28+
.setCacheControl(noCache);
29+
30+
registry.addResourceHandler("/static/media/**")
31+
.addResourceLocations(staticLocations)
32+
.setCacheControl(dayCache);
33+
34+
registry.addResourceHandler("/static/js/**", "/static/css/**")
35+
.addResourceLocations(staticLocations)
36+
.setCacheControl(weekCache);
37+
38+
registry.addResourceHandler(
39+
"/*.png",
40+
"/*.jpg",
41+
"/*.jpeg",
42+
"/*.gif",
43+
"/*.webp",
44+
"/*.svg",
45+
"/*.ico"
46+
)
47+
.addResourceLocations(staticLocations)
48+
.setCacheControl(dayCache);
49+
}
50+
}

app/server/huntly-server/src/main/java/com/huntly/server/controller/ReactAppController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
public class ReactAppController {
1313
@RequestMapping(value = {"/", "/{x:[\\w\\-]+}", "/{x:^(?!api$).*$}/**/{y:[\\w\\-]+}"})
1414
public String getIndex() {
15-
return "/index.html";
15+
return "forward:/index.html";
1616
}
1717
}

app/server/huntly-server/src/main/resources/application.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ spring:
1616
mvc:
1717
pathmatch:
1818
matching-strategy: ant_path_matcher # use this to make springfox work
19-
web:
20-
resources:
21-
cache:
22-
cachecontrol:
23-
max-age: 7d
2419

2520
huntly:
2621
jwtSecret: MTI2ZTc1NzAtMjJlMy00MmVlLTkwYmQtOTVjNGM4ZTRhN2YzMTI2ZTc1NzAtMjJlMy00MmVlLTkwYmQtOTVjNGM4ZTRhN2Yz

0 commit comments

Comments
 (0)