Skip to content

Commit eae1c3d

Browse files
committed
fix: frontend cache issue
1 parent e53a4fc commit eae1c3d

2 files changed

Lines changed: 37 additions & 11 deletions

File tree

backend/src/app.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import passport from "./config/passport.config.js";
77
import errorHandler from "./middlewares/errorHandler.middlewares.js";
88
import { requestLogger } from "./middlewares/requestLogger.middlewares.js";
99
import routes from "./api.routes.js";
10+
import { serveFrontend } from "./static/serveFrontend.js";
1011

1112
const __filename = fileURLToPath(import.meta.url);
1213
const __dirname = path.dirname(__filename);
@@ -21,20 +22,10 @@ app.use(requestLogger);
2122
app.use(passport.initialize());
2223
app.use(express.json());
2324
app.use(cookieParser());
24-
app.use(express.static(clientDistPath));
2525

2626
app.use("/api/v1", routes);
2727

28-
app.get("/*splat", (req, res, next) => {
29-
if (req.path.startsWith("/api/")) {
30-
return next();
31-
}
32-
res.sendFile(path.join(clientDistPath, "index.html"), (err) => {
33-
if (err) {
34-
next();
35-
}
36-
});
37-
});
28+
serveFrontend(app, clientDistPath);
3829

3930
app.use(errorHandler);
4031

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import express from "express";
2+
import path from "path";
3+
4+
export function serveFrontend(app: express.Express, clientDistPath: string) {
5+
app.use(
6+
"/assets",
7+
express.static(path.join(clientDistPath, "assets"), {
8+
immutable: true,
9+
maxAge: "1y",
10+
etag: false,
11+
}),
12+
);
13+
14+
app.use(
15+
express.static(clientDistPath, {
16+
etag: false,
17+
maxAge: 0,
18+
setHeaders(res, filePath) {
19+
if (filePath.endsWith(".html")) {
20+
res.setHeader("Cache-Control", "public, max-age=0, must-revalidate");
21+
}
22+
},
23+
}),
24+
);
25+
26+
app.get("/*splat", (_req, res, next) => {
27+
res.setHeader("Cache-Control", "public, max-age=0, must-revalidate");
28+
29+
res.sendFile(path.join(clientDistPath, "index.html"), (err) => {
30+
if (err) {
31+
next(err);
32+
}
33+
});
34+
});
35+
}

0 commit comments

Comments
 (0)