Skip to content

Commit 76e6ea3

Browse files
committed
Refactor static assets for better caching / fingerprinting
1 parent 8a6d2f8 commit 76e6ea3

File tree

11 files changed

+52
-21
lines changed

11 files changed

+52
-21
lines changed

stubbornjava-common/src/main/java/com/stubbornjava/common/undertow/handlers/CustomHandlers.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.io.File;
55
import java.nio.file.Paths;
66
import java.util.SortedMap;
7-
import java.util.concurrent.TimeUnit;
87

98
import org.slf4j.Logger;
109
import org.slf4j.LoggerFactory;
@@ -57,7 +56,7 @@ public static HttpHandler gzip(HttpHandler next) {
5756
.setNext(next);
5857
}
5958

60-
public static HttpHandler resource(String prefix) {
59+
public static HttpHandler resource(String prefix, int cacheTime) {
6160
ResourceManager resourceManager = null;
6261
if (Env.LOCAL == Env.get()) {
6362
String path = Paths.get(AssetsConfig.assetsRoot(), prefix).toString();
@@ -68,7 +67,7 @@ public static HttpHandler resource(String prefix) {
6867
resourceManager = new ClassPathResourceManager(CustomHandlers.class.getClassLoader(), prefix);
6968
}
7069
ResourceHandler handler = new ResourceHandler(resourceManager);
71-
handler.setCacheTime((int)TimeUnit.HOURS.toSeconds(4));
70+
handler.setCacheTime(cacheTime);
7271
return handler;
7372
}
7473

stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/StubbornJavaWebApp.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import static com.stubbornjava.common.undertow.handlers.CustomHandlers.timed;
44

5+
import java.util.concurrent.TimeUnit;
6+
57
import org.slf4j.Logger;
68
import org.slf4j.LoggerFactory;
79

@@ -43,7 +45,7 @@ private static HttpHandler wrapWithMiddleware(HttpHandler handler) {
4345
.get("/", timed("getHome", PageRoutes::home))
4446
.get("/ping", timed("ping", PageRoutes::ping))
4547

46-
.get("/favicon.ico", timed("favicon", CustomHandlers.resource("images/")))
48+
.get("/favicon.ico", timed("favicon", CustomHandlers.resource("images/", (int)TimeUnit.DAYS.toSeconds(30))))
4749
.get("robots.txt", timed("robots", PageRoutes::robots))
4850

4951
.get("/posts/{slug}", timed("getPost", PostRoutes::getPost))
@@ -67,10 +69,7 @@ private static HttpHandler wrapWithMiddleware(HttpHandler handler) {
6769
;
6870

6971
private static final HttpHandler staticRoutes = new PathHandler(basicRoutes)
70-
.addPrefixPath("/css", timed("getCss", CustomHandlers.resource("css/")))
71-
.addPrefixPath("/js", timed("getJs", CustomHandlers.resource("js/")))
72-
.addPrefixPath("/assets/fonts", timed("getJs", CustomHandlers.resource("fonts/")))
73-
.addPrefixPath("/images", timed("getImages", CustomHandlers.resource("images/")))
72+
.addPrefixPath("/assets", timed("getAssets", CustomHandlers.resource("", (int)TimeUnit.DAYS.toSeconds(30))))
7473
;
7574

7675
private static void startServer() {

stubbornjava-webapp/src/main/resources/application.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
assets.root = "ui/assets"
2+
13
db {
24
url="jdbc:mysql://localhost:3306/stubbornjava"
35
driver="com.mysql.jdbc.Driver"

stubbornjava-webapp/ui/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"style-loader": "^0.13.1",
3131
"url-loader": "^0.5.7",
3232
"webpack": "^2.2.1",
33+
"webpack-clean-obsolete-chunks": "^0.3.0",
3334
"webpack-config": "^7.0.0"
3435
},
3536
"scripts": {}

stubbornjava-webapp/ui/src/common/head.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
<meta http-equiv="x-ua-compatible" content="ie=edge"/>
44
<meta name="description" content="{{#if metaDesc }}{{metaDesc}}{{/if}}"/>
55
{{!-- <link rel="stylesheet" href="/css/3rdparty.css"/>--}}
6-
<link rel="stylesheet" href="/css/common.css"/>
6+
<link rel="stylesheet" href="/assets/css/common.css"/>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{{!-- <script async type="text/javascript" src="/js/3rdparty.js"></script> --}}
22
{{!-- Making this script async breaks the jump to id functionality :( --}}
3-
<script type="text/javascript" src="/js/common.js"></script>
3+
<script type="text/javascript" src="/assets/js/common.js"></script>

stubbornjava-webapp/ui/src/posts/webpack-and-npm-for-simple-java-8-web-apps.hbs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,22 @@
4444
<p>You should be able to clone the repo and run the <code>WebpackServer</code> example. As long as you run <code>webpack --progress --watch</code> everything should work locally. If there are issues loading assets try changing the <code>assets.root</code> property found <a href="https://github.com/StubbornJava/StubbornJava/blob/master/stubbornjava-examples/src/main/resources/application.conf#L2">here</a>. This is a hack to make files load faster in the IDE, for some reason even with native hooks turned on the IDE's are slow to pick up file changes. Here are screen shots of the four pages. If you are not a strong HTML / CSS developer consider using a <a href="/best-selling-html-css-themes-and-website-templates">site template</a> to make your website stand out.</p>
4545

4646
<h2 class="anchored">home</h2>
47-
<div><img width=100% src="/images/homepage.png" /></div>
47+
<div><img width=100% src="/assets/images/homepage.png" /></div>
4848
<hr/>
4949
<br/>
5050
<br/>
5151
<br/>
5252
<h2 class="anchored">hello {name}</h2>
53-
<img width=100% src="/images/hello.png" />
53+
<img width=100% src="/assets/images/hello.png" />
5454
<hr/>
5555
<br/>
5656
<br/>
5757
<br/>
5858
<h2 class="anchored">Not Found</h2>
59-
<img width=100% src="/images/notfound.png" />
59+
<img width=100% src="/assets/images/notfound.png" />
6060
<hr/>
6161
<br/>
6262
<br/>
6363
<br/>
6464
<h2 class="anchored">Server Error</h2>
65-
<img width=100% src="/images/error.png" />
65+
<img width=100% src="/assets/images/error.png" />
17.8 KB
Loading

stubbornjava-webapp/ui/src/widgets/nav/header.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<nav class="navbar navbar-inverse navbar-expand fixed-top" role="navigation">
22
<a class="navbar-brand logo" href="/">
3-
<span class="align-middle"><img src="/images/PenguinHQ.png" height="40px"> Stubborn<span class="java">Java</span></span>
3+
<span class="align-middle"><img src="/assets/images/PenguinHQ.png" height="40px"> Stubborn<span class="java">Java</span></span>
44
{{!-- Add this SVG instead later. I can't get CSS to work --}}
55
{{!-- <span>{{> templates/src/widgets/nav/logo }} Stubborn<span class="java">Java</span></span> --}}
66
</a>

stubbornjava-webapp/ui/src/widgets/social/twitter/summary-card.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
<meta name="twitter:site" content="@StubbornJava" />
33
<meta name="twitter:title" content="{{title}}" />
44
<meta name="twitter:description" content="{{desc}}" />
5-
<meta name="twitter:image" content="/images/PenguinHQ.png" />
5+
<meta name="twitter:image" content="https://www.stubbornjava.com/assets/images/PenguinHQ_compressed.png" />

0 commit comments

Comments
 (0)