miniSearch / server /cacheServerHook.ts
github-actions[bot]
Sync to HuggingFace Spaces
f152ae2
raw
history blame contribute delete
758 Bytes
import { Pattern, match } from "ts-pattern";
import type { PreviewServer, ViteDevServer } from "vite";
export function cacheServerHook<T extends ViteDevServer | PreviewServer>(
server: T,
) {
server.middlewares.use(async (request, response, next) => {
const cacheControlValue = match(request.url)
.with(
Pattern.string.startsWith("/assets/"),
() => "public, max-age=31536000, immutable",
)
.with(
Pattern.union(
"/",
Pattern.string.startsWith("/?"),
Pattern.string.endsWith(".html"),
),
() => "no-cache",
)
.otherwise(() => "public, max-age=86400, must-revalidate");
response.setHeader("Cache-Control", cacheControlValue);
next();
});
}