dokploy/apps/vite/server/static.ts
Mauricio Siu a7adc3c36b refactor: update Vite application structure and dependencies
- Removed the `index.html` file as part of the transition to a new routing system.
- Updated `package.json` scripts to streamline development commands using `concurrently`.
- Enhanced dependency management by adding new packages and updating existing ones in `package.json`.
- Introduced a new server function for branding and session management, improving API interactions.
- Refactored routing to utilize TanStack Start, ensuring better integration with the existing Dokploy architecture.
- Improved static file handling and server response management in the custom Node.js server.
- Updated README to reflect changes in architecture and development processes.
2026-08-11 01:15:43 -06:00

59 lines
1.6 KiB
TypeScript

import fs from "node:fs";
import type { IncomingMessage, ServerResponse } from "node:http";
import path from "node:path";
const MIME_TYPES: Record<string, string> = {
".html": "text/html; charset=utf-8",
".js": "text/javascript; charset=utf-8",
".mjs": "text/javascript; charset=utf-8",
".css": "text/css; charset=utf-8",
".json": "application/json; charset=utf-8",
".svg": "image/svg+xml",
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".gif": "image/gif",
".webp": "image/webp",
".avif": "image/avif",
".ico": "image/x-icon",
".txt": "text/plain; charset=utf-8",
".woff": "font/woff",
".woff2": "font/woff2",
".ttf": "font/ttf",
".map": "application/json; charset=utf-8",
".wasm": "application/wasm",
".webmanifest": "application/manifest+json",
};
export const createStaticHandler = (distDir: string) => {
return (req: IncomingMessage, res: ServerResponse, url: URL) => {
if (req.method !== "GET" && req.method !== "HEAD") {
res.writeHead(404).end();
return;
}
const pathname = decodeURIComponent(url.pathname);
const filePath = path.join(distDir, pathname);
if (!filePath.startsWith(distDir)) {
res.writeHead(403).end();
return;
}
if (!fs.existsSync(filePath) || !fs.statSync(filePath).isFile()) {
res.writeHead(404).end();
return;
}
const cacheControl = pathname.startsWith("/assets/")
? "public, max-age=31536000, immutable"
: "public, max-age=0, must-revalidate";
res.writeHead(200, {
"content-type":
MIME_TYPES[path.extname(filePath).toLowerCase()] ??
"application/octet-stream",
"cache-control": cacheControl,
});
fs.createReadStream(filePath).pipe(res);
};
};