Files
library/api/src/app/plugins/static.ts
T
hikari e6b6131134 fix: allow static assets to be served correctly
Fixed the not-found handler to exclude /assets routes, allowing static assets
like images to be served with correct MIME types instead of being caught by
the SPA fallback handler.

Changes:
- Updated setNotFoundHandler to check for /assets prefix
- Assets are now served directly by fastify-static with correct content-type
- Only non-API, non-asset routes fall through to index.html

This fixes the default cover image not displaying because it was being served
as text/html instead of image/jpeg.

Co-Authored-By: Naomi Carrigan <commits@nhcarrigan.com>
2026-02-20 19:45:37 -08:00

32 lines
1.0 KiB
TypeScript

/**
* @copyright 2026 NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { FastifyInstance } from "fastify";
import fastifyStatic from "@fastify/static";
import path from "path";
export default async function staticPlugin(app: FastifyInstance) {
const isProduction = process.env.NODE_ENV === "production";
if (isProduction) {
// Serve the built Angular app from dist directory
await app.register(fastifyStatic, {
root: path.join(__dirname, "../../../../../../dist/apps/frontend"),
prefix: "/", // Serve at root
wildcard: false, // Disable wildcard routes to avoid conflicts
});
// Catch-all route for Angular SPA routing (must be registered after API routes)
app.setNotFoundHandler((request, reply) => {
// Only catch routes that don't start with /api or /assets
if (!request.url.startsWith("/api") && !request.url.startsWith("/assets")) {
reply.sendFile("index.html");
} else {
reply.code(404).send({ error: "Not Found" });
}
});
}
}