b4e7cce651
continuous-integration/drone/push Build is passing
- Add proper cache control headers in static.ts - HTML files: no-cache to always get fresh version - Assets: long cache with immutable (content-hashed filenames) - Improve typing indicator animation (larger dots, faster animation)
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import express, { type Express } from "express";
|
|
import fs from "fs";
|
|
import path from "path";
|
|
|
|
export function log(message: string, source = "express") {
|
|
const formattedTime = new Date().toLocaleTimeString("en-US", {
|
|
hour: "numeric",
|
|
minute: "2-digit",
|
|
second: "2-digit",
|
|
hour12: true,
|
|
});
|
|
|
|
console.log(`${formattedTime} [${source}] ${message}`);
|
|
}
|
|
|
|
export function serveStatic(app: Express) {
|
|
const distPath = path.resolve(import.meta.dirname, "public");
|
|
|
|
if (!fs.existsSync(distPath)) {
|
|
throw new Error(
|
|
`Could not find the build directory: ${distPath}, make sure to build the client first`,
|
|
);
|
|
}
|
|
|
|
// Serve static assets with long cache (they have content hashes in filenames)
|
|
app.use("/assets", express.static(path.join(distPath, "assets"), {
|
|
maxAge: '1y',
|
|
immutable: true
|
|
}));
|
|
|
|
// Serve other static files with no-cache for HTML
|
|
app.use(express.static(distPath, {
|
|
setHeaders: (res, filePath) => {
|
|
if (filePath.endsWith('.html')) {
|
|
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
|
|
}
|
|
}
|
|
}));
|
|
|
|
// fall through to index.html if the file doesn't exist (with no-cache)
|
|
app.use("*", (_req, res) => {
|
|
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
|
|
res.sendFile(path.resolve(distPath, "index.html"));
|
|
});
|
|
}
|