all repos — stealth-developers @ 08f8a3e5f9e51e0bd471256b6990af8a4ab6ff77

api: serve frontend correctly
vi did:web:vt3e.cat
Wed, 01 Jul 2026 01:06:06 +0100
commit

08f8a3e5f9e51e0bd471256b6990af8a4ab6ff77

parent

531ec5ea277d14986c56752b00aaebf21ce834ec

1 files changed, 29 insertions(+), 10 deletions(-)

jump to
M apps/api/src/app.tsapps/api/src/app.ts

@@ -1,10 +1,10 @@

import cors from "@elysia/cors"; import openapi from "@elysia/openapi"; import serverTiming from "@elysia/server-timing"; -import { staticPlugin } from "@elysia/static"; import config from "@stealth-developers/config"; import { getUser } from "@stealth-developers/db"; import { Elysia, t } from "elysia"; +import { join } from "node:path"; import { authPlugin } from "./lib/auth"; import { getLinkedActors } from "./lib/db";

@@ -13,7 +13,6 @@ import { ActorWithGuildSchema, ErrorSchema, UserSchema } from "./lib/schemas";

import * as routes from "./routes"; export const app = new Elysia() - .onError(({ error, set, request }) => { set.status = 500; console.error(request.url, error);

@@ -23,13 +22,6 @@ return { message: "Unknown Error" };

}) .use(openapi()) .use(cors()) - .use( - staticPlugin({ - assets: config.api.publicDir, - indexHTML: true, - prefix: "/", - }), - ) .use(authPlugin) .use(serverTiming()) .use(routes.authRoutes)

@@ -67,7 +59,34 @@ tags: ["auth"],

}, ) .use(routes.guildRoutes), - ); + ) + .get("*", async ({ path, set }) => { + const isExcluded = + path === "/api" || path.startsWith("/api/") || path === "/auth" || path.startsWith("/auth/"); + + if (isExcluded) { + set.status = 404; + return { message: "Not Found" }; + } + + if (path.includes("..")) { + set.status = 400; + return { message: "Bad Request" }; + } + + if (path !== "/") { + const staticFile = Bun.file(join(config.api.publicDir, path)); + if (await staticFile.exists()) { + if (path.startsWith("/assets/")) { + set.headers["cache-control"] = "public, max-age=31536000, immutable"; + } + return staticFile; + } + } + + set.headers["cache-control"] = "no-store, no-cache, must-revalidate, proxy-revalidate"; + return Bun.file(join(config.api.publicDir, "index.html")); + }); export type App = typeof app; export type {