File size: 646 Bytes
b44ec47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import type { PreviewServer, ViteDevServer } from "vite";

export function cacheServerHook<T extends ViteDevServer | PreviewServer>(
  server: T,
) {
  server.middlewares.use(async (request, response, next) => {
    let cacheControlValue = "public, max-age=86400, must-revalidate";
    if (request.url.startsWith("/assets/")) {
      cacheControlValue = "public, max-age=31536000, immutable";
    } else if (
      request.url === "/" ||
      request.url.startsWith("/?") ||
      request.url.endsWith(".html")
    ) {
      cacheControlValue = "no-cache";
    }

    response.setHeader("Cache-Control", cacheControlValue);

    next();
  });
}