Skip to content

Commit e6a37f0

Browse files
committed
fix(backend): fix unexpected behavior of static file serving
1 parent dd845c3 commit e6a37f0

3 files changed

Lines changed: 39 additions & 42 deletions

File tree

backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"typebox": "^0.0.1"
2626
},
2727
"scripts": {
28-
"dev": "COMMIT_SHA=$(git rev-parse HEAD) CONSOLA_LEVEL=9999 bun --watch --hot --no-clear-screen --inspect src/index.ts",
28+
"dev": "COMMIT_SHA=$(git rev-parse HEAD) CONSOLA_LEVEL=9999 FRONTEND_DIR=../frontend/dist bun --watch --hot --no-clear-screen --inspect src/index.ts",
2929
"build": "NODE_ENV=production bun build src/index.ts --target bun --outdir out/",
3030
"format": "biome format --write",
3131
"check": "biome check --write"

backend/src/index.ts

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,16 @@ import { swagger } from "@elysiajs/swagger";
44
import { serverTiming } from "@elysiajs/server-timing";
55
import { routes } from "@/api";
66
import { loggerPlugin } from "@/plugins/loggerPlugin";
7-
import { ALLOWED_ORIGINS, PORT, PRODUCTION } from "@/utils/config";
7+
import { ALLOWED_ORIGINS, PORT, PRODUCTION, FRONTEND_DIR } from "@/utils/config";
88
import { consola } from "consola";
99
import { initConfig } from "./utils/init";
1010
import { staticPlugin } from "@elysiajs/static";
1111
import { exists, readFile } from "node:fs/promises"
12+
import { join, resolve } from "node:path";
1213

1314
await initConfig();
1415

15-
const staticRoute = await (async () => {
16-
let elysia = new Elysia();
17-
if (await exists("dist")) {
18-
elysia = elysia.use(staticPlugin({
19-
assets: "dist",
20-
alwaysStatic: true,
21-
prefix: "/",
22-
}))
23-
if (await exists("dist/index.html")) {
24-
const indexHtml = await readFile("dist/index.html", "utf-8");
25-
elysia = elysia.get("/*", ({ headers: { accept } }) => {
26-
if (typeof accept === "string" && !accept.includes("text/html")) {
27-
return new Response("Not Found", {
28-
status: 404,
29-
});
30-
}
31-
return new Response(indexHtml, {
32-
headers: {
33-
"Content-Type": "text/html",
34-
},
35-
});
36-
}, {
37-
headers: t.Object({
38-
accept: t.Optional(t.String())
39-
})
40-
})
41-
}
42-
}
43-
return elysia;
44-
})()
45-
46-
const app = new Elysia()
16+
let app = new Elysia()
4717
.use(loggerPlugin)
4818
.use(
4919
cors({
@@ -75,14 +45,39 @@ const app = new Elysia()
7545
}),
7646
)
7747
.use(serverTiming())
78-
.use(routes)
79-
.use(staticRoute)
80-
.listen({
81-
port: PORT,
82-
reusePort: PRODUCTION,
83-
hostname: "0.0.0.0",
84-
idleTimeout: 255,
85-
});
48+
.use(routes);
49+
50+
if (await exists(FRONTEND_DIR)) {
51+
const dir = resolve(FRONTEND_DIR);
52+
consola.log(`Setting up static file serving from ${dir}`);
53+
app = app.use(staticPlugin({
54+
assets: dir,
55+
alwaysStatic: true,
56+
prefix: "/",
57+
}))
58+
if (await exists(join(dir, "index.html"))) {
59+
const indexHtml = await readFile(join(dir, "index.html"), "utf-8");
60+
app = app.get("/*", ({ headers: { accept } }) => {
61+
if (typeof accept === "string" && !accept.includes("text/html")) {
62+
return new Response("Not Found", {
63+
status: 404,
64+
});
65+
}
66+
return new Response(indexHtml, {
67+
headers: {
68+
"Content-Type": "text/html",
69+
},
70+
});
71+
});
72+
}
73+
}
74+
75+
app = app.listen({
76+
port: PORT,
77+
reusePort: PRODUCTION,
78+
hostname: "0.0.0.0",
79+
idleTimeout: 255,
80+
});
8681

8782
consola.log(`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`);
8883

backend/src/utils/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,5 @@ export const FORCILY_ADD_API_KEYS = env(
105105
"forcily add api keys",
106106
zObject(forcilyAddApiKeysSchema.optional()),
107107
);
108+
109+
export const FRONTEND_DIR = env("frontend dir", z.coerce.string(), "dist")

0 commit comments

Comments
 (0)