feat(server): 单域托管 client/dist 与 SPA 回退(E12)
- 新增 SERVE_CLIENT_DIST/CLIENT_DIST_DIR 配置 - 开启后注册 @fastify/static 并对非 /api GET(html) 回退 index.html - docker-compose.yml 与 README 补充用法
这个提交包含在:
+20
-1
@@ -9,6 +9,7 @@ const cookie = require("@fastify/cookie");
|
||||
const multipart = require("@fastify/multipart");
|
||||
const helmet = require("@fastify/helmet");
|
||||
const rateLimit = require("@fastify/rate-limit");
|
||||
const staticPlugin = require("@fastify/static");
|
||||
|
||||
const { getConfig } = require("./config");
|
||||
const authPlugin = require("./plugins/auth");
|
||||
@@ -95,11 +96,29 @@ async function buildApp() {
|
||||
},
|
||||
});
|
||||
|
||||
if (config.serveClientDist) {
|
||||
await app.register(staticPlugin, {
|
||||
root: config.clientDistDir,
|
||||
});
|
||||
app.log.info({ dir: config.clientDistDir }, "Client dist static serving enabled");
|
||||
}
|
||||
|
||||
app.addHook("onRequest", async (request, reply) => {
|
||||
reply.header("x-request-id", request.id);
|
||||
});
|
||||
|
||||
app.setNotFoundHandler(async () => {
|
||||
app.setNotFoundHandler(async (request, reply) => {
|
||||
if (
|
||||
config.serveClientDist &&
|
||||
request.method === "GET" &&
|
||||
!request.url.startsWith("/api") &&
|
||||
typeof request.headers.accept === "string" &&
|
||||
request.headers.accept.includes("text/html") &&
|
||||
typeof reply.sendFile === "function"
|
||||
) {
|
||||
return reply.type("text/html").sendFile("index.html");
|
||||
}
|
||||
|
||||
throw new AppError({
|
||||
code: "SYS_404",
|
||||
message: "Not Found",
|
||||
|
||||
在新工单中引用
屏蔽一个用户