Skip to content

Commit a4f0c40

Browse files
committed
cache static assets
1 parent 7348139 commit a4f0c40

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

apps/server/src/main.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ if (env.PUBLIC_DIR) {
9696
await server.register(import("@fastify/static"), {
9797
root: publicDir,
9898
prefix: "/",
99+
setHeaders(res, path) {
100+
if (path.startsWith(`${publicDir}/assets/`)) {
101+
// Cache assets for 1 year
102+
res.setHeader("Cache-Control", "public, max-age=31536000, immutable");
103+
} else if (path.endsWith(".html")) {
104+
// Do not cache HTML files
105+
res.setHeader("Cache-Control", "no-cache");
106+
} else {
107+
// Cache other files for 1 hour
108+
res.setHeader("Cache-Control", "public, max-age=3600");
109+
}
110+
},
99111
});
100112

101113
server.setNotFoundHandler((_request, reply) => {

0 commit comments

Comments
 (0)