import type { ServeStaticModuleOptions } from "@nestjs/serve-static"; import type { ServerResponse } from "node:http"; import * as path from "node:path"; /** Server routes that must never fall back to the client's index.html. */ const SERVER_ROUTES = [ "/api/{*path}", "/mcp{*path}", "/docs{*path}", "/health", "/healthz", ]; /** * Serves the built client (SPA) from `clientDir`: static files as they are, * any other GET outside the server routes gets index.html so client-side * routes survive a page refresh. Returns no options (nothing served) when * `clientDir` is empty, e.g. in tests and local development. */ export function clientStaticOptions( clientDir: string, ): ServeStaticModuleOptions[] { if (!clientDir) return []; return [ { rootPath: path.resolve(clientDir), exclude: SERVER_ROUTES, serveStaticOptions: { setHeaders: (res: ServerResponse, filePath: string) => { // Vite emits content-hashed files under assets/; index.html must // always be revalidated so a new deploy is picked up immediately const cacheControl = filePath.includes(`${path.sep}assets${path.sep}`) ? "public, max-age=31536000, immutable" : "no-cache"; res.setHeader("Cache-Control", cacheControl); }, }, }, ]; }