fix: production routing for auth callbacks and restore API type inference

This commit is contained in:
bipproduction
2026-02-07 17:21:20 +08:00
parent 5c2bd971d7
commit d2abd9dafb

View File

@@ -14,9 +14,17 @@ const isProduction = process.env.NODE_ENV === "production";
const api = new Elysia({ const api = new Elysia({
prefix: "/api", prefix: "/api",
}) })
.all("/auth/*", ({ request }) => auth.handler(request))
.use(cors()) .use(cors())
.use( .all("/auth/*", ({ request }) => auth.handler(request))
.get("/session", async ({ request }) => {
const data = await auth.api.getSession({ headers: request.headers });
return { data };
})
.use(apiMiddleware)
.use(apikey);
if (!isProduction) {
api.use(
swagger({ swagger({
path: "/docs", path: "/docs",
documentation: { documentation: {
@@ -26,13 +34,8 @@ const api = new Elysia({
}, },
}, },
}), }),
) );
.get("/session", async ({ request }) => { }
const data = await auth.api.getSession({ headers: request.headers });
return { data };
})
.use(apiMiddleware)
.use(apikey);
const app = new Elysia().use(api); const app = new Elysia().use(api);
@@ -149,28 +152,30 @@ if (!isProduction) {
}); });
}); });
} else { } else {
// Production: Serve static files from dist // Production: Final catch-all for static files and SPA fallback
app.get("*", async ({ request }) => { app.all("*", async ({ request }) => {
const url = new URL(request.url); const url = new URL(request.url);
let pathname = url.pathname; const pathname = url.pathname;
// Skip API routes // 1. Try exact match in dist
if (pathname.startsWith("/api")) { let filePath = path.join("dist", pathname === "/" ? "index.html" : pathname);
return new Response("Not Found", { status: 404 });
// 2. If not found and looks like an asset (has extension), try root of dist
if (!fs.existsSync(filePath) || !fs.statSync(filePath).isFile()) {
if (pathname.includes(".") && !pathname.endsWith("/")) {
const filename = path.basename(pathname);
const fallbackPath = path.join("dist", filename);
if (fs.existsSync(fallbackPath) && fs.statSync(fallbackPath).isFile()) {
filePath = fallbackPath;
}
}
} }
if (pathname === "/") {
pathname = "/index.html";
}
const filePath = path.join("dist", pathname);
if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) { if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
const file = Bun.file(filePath); return new Response(Bun.file(filePath));
return new Response(file);
} }
// SPA Fallback // 3. SPA Fallback: Serve index.html
const indexHtml = path.join("dist", "index.html"); const indexHtml = path.join("dist", "index.html");
if (fs.existsSync(indexHtml)) { if (fs.existsSync(indexHtml)) {
return new Response(Bun.file(indexHtml)); return new Response(Bun.file(indexHtml));