-
Notifications
You must be signed in to change notification settings - Fork 82
feat(webui): Add cache control for static files. #1235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,12 +1,45 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import path from "node:path"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import {fileURLToPath} from "node:url"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import {fastifyStatic} from "@fastify/static"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import fastifyStatic from "@fastify/static"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import {FastifyPluginAsync} from "fastify"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import settings from "../../settings.json" with {type: "json"}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const CACHE_CONTROL_HEADER_KEY = "Cache-Control"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const CACHE_CONTROL_HEADER_VALUE_NO_CACHE = "public, max-age=0"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Cache all other static files for 1 year without revalidation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const CACHE_CONTROL_HEADER_VALUE_LONG_TERM_CACHE = "public, max-age=31536000, immutable"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+10
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Optional: strengthen revalidation for no-cache responses -const CACHE_CONTROL_HEADER_VALUE_NO_CACHE = "public, max-age=0";
+const CACHE_CONTROL_HEADER_VALUE_NO_CACHE = "public, max-age=0, must-revalidate"; 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* Configures `Cache-Control` header for static files to reduce network traffic. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* @param res | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* @param reqPath | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* @param extraNoCachePaths | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const setCacheHeaders = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res: Parameters<NonNullable<fastifyStatic.FastifyStaticOptions["setHeaders"]>>[0], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the proper type name instead of fastifyStatic.FastifyStaticOptions This reference will fail under default import; switch to the named type. - res: Parameters<NonNullable<fastifyStatic.FastifyStaticOptions["setHeaders"]>>[0],
+ res: Parameters<NonNullable<FastifyStaticOptions["setHeaders"]>>[0], 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
reqPath: string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
extraNoCachePaths: string[] = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
): void => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const noCachePaths = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"/index.html", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
...extraNoCachePaths, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (noCachePaths.some((noCachePath) => reqPath.endsWith(noCachePath))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res.setHeader(CACHE_CONTROL_HEADER_KEY, CACHE_CONTROL_HEADER_VALUE_NO_CACHE); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res.setHeader(CACHE_CONTROL_HEADER_KEY, CACHE_CONTROL_HEADER_VALUE_LONG_TERM_CACHE); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+24
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Make no-cache matching OS-agnostic (Windows path separators) const setCacheHeaders = (
res: Parameters<NonNullable<FastifyStaticOptions["setHeaders"]>>[0],
reqPath: string,
extraNoCachePaths: string[] = []
): void => {
+ // Normalise to POSIX-style separators so suffix checks work on all OSes
+ const normalizedReqPath = reqPath.split(path.win32.sep).join("/");
const noCachePaths = [
"/index.html",
...extraNoCachePaths,
];
- if (noCachePaths.some((noCachePath) => reqPath.endsWith(noCachePath))) {
+ if (noCachePaths.some((noCachePath) => normalizedReqPath.endsWith(noCachePath))) {
res.setHeader(CACHE_CONTROL_HEADER_KEY, CACHE_CONTROL_HEADER_VALUE_NO_CACHE);
return;
}
res.setHeader(CACHE_CONTROL_HEADER_KEY, CACHE_CONTROL_HEADER_VALUE_LONG_TERM_CACHE);
}; 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* Creates static files serving routes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -40,21 +73,32 @@ const routes: FastifyPluginAsync = async (fastify) => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
logViewerDir = path.resolve(rootDirname, logViewerDir); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
await fastify.register(fastifyStatic, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
decorateReply: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
prefix: "/log-viewer", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
root: logViewerDir, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
decorateReply: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Prevent fastify-static from adding its own cache headers and provide our own. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cacheControl: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setHeaders: (res, reqPath) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setCacheHeaders(res, reqPath); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+80
to
85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Minor TS ergonomics: include the third ‘stat’ parameter for signature parity - setHeaders: (res, reqPath) => {
- setCacheHeaders(res, reqPath);
- },
+ setHeaders: (res, reqPath, _stat) => {
+ setCacheHeaders(res, reqPath);
+ }, 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let clientDir = settings.ClientDir; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (false === path.isAbsolute(clientDir)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
clientDir = path.resolve(rootDirname, settings.ClientDir); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
await fastify.register(fastifyStatic, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
decorateReply: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
prefix: "/", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
root: clientDir, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
decorateReply: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
wildcard: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Prevent fastify-static from adding its own cache headers and provide our own. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cacheControl: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setHeaders: (res, reqPath) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setCacheHeaders(res, reqPath, ["/settings.json"]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+97
to
102
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Same as above: include third ‘stat’ param and simplify wrapper - setHeaders: (res, reqPath) => {
- setCacheHeaders(res, reqPath, ["/settings.json"]);
- },
+ setHeaders: (res, reqPath, _stat) => {
+ setCacheHeaders(res, reqPath, ["/settings.json"]);
+ }, 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Serve index.html for all unmatched routes in the React Single Page Application (SPA). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix type reference for FastifyStaticOptions — default import doesn’t expose types
fastifyStatic.FastifyStaticOptions
will not resolve when using a default import. Bring the type in via a separate type-only import to avoid a TS compile error.Apply this diff to add the type import:
import fastifyStatic from "@fastify/static"; +import type { FastifyStaticOptions } from "@fastify/static";
📝 Committable suggestion
🤖 Prompt for AI Agents