Files
SnapOtter/apps/api/src/plugins/static.ts
T
SnapOtter 66e503730d fix: resolve 6 production Sentry errors
- Prevent @fastify/static double-registration crash via decorateReply guard
- Fix non-ASCII filename header encoding (X-Output-Filename + RFC 5987 Content-Disposition)
- Add EACCES error handling to all startup mkdir calls with actionable messages
- Add WAL autocheckpoint and journal size limit to prevent unbounded SQLite growth
- Fix Python sidecar EPIPE handling to reject pending requests and trigger restart
- Ensure Docker entrypoint creates all subdirectories before chown
2026-06-06 16:05:59 +08:00

33 lines
1.0 KiB
TypeScript

import { existsSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import fastifyStatic from "@fastify/static";
import type { FastifyInstance } from "fastify";
export async function registerStatic(app: FastifyInstance) {
// Resolve relative to this file's location
const __dirname = dirname(fileURLToPath(import.meta.url));
const webDistPath = resolve(__dirname, "../../../web/dist");
if (!existsSync(webDistPath)) {
app.log.warn(`SPA dist not found at ${webDistPath} — skipping static file serving`);
return;
}
await app.register(fastifyStatic, {
root: webDistPath,
prefix: "/",
wildcard: false,
decorateReply: !app.hasReplyDecorator("sendFile"),
});
// SPA fallback — serve index.html for all non-API routes
app.setNotFoundHandler((request, reply) => {
if (request.url.startsWith("/api/")) {
reply.code(404).send({ error: "Not found", code: "NOT_FOUND" });
} else {
reply.sendFile("index.html");
}
});
}