Jelajahi Sumber

♻️ refactor(server): adjust middleware order for better request flow

- move API route middleware after the common request processing middleware
- maintain consistent API handling logic while improving request processing sequence

📦 build(deps): add vite-plugin-iframe-communicator dependency

- install vite-plugin-iframe-communicator@^0.0.1 to enable iframe communication capabilities
- update pnpm-lock.yaml with new dependency and its resolution
yourname 4 bulan lalu
induk
melakukan
92266000fc
3 mengubah file dengan 60 tambahan dan 46 penghapusan
  1. 1 0
      package.json
  2. 12 0
      pnpm-lock.yaml
  3. 47 46
      server.js

+ 1 - 0
package.json

@@ -55,6 +55,7 @@
     "tsx": "^4.20.3",
     "typescript": "~5.8.3",
     "vite": "^7.0.0",
+    "vite-plugin-iframe-communicator": "^0.0.1",
     "vite-progress-tracking-plugin": "^0.0.2"
   }
 }

+ 12 - 0
pnpm-lock.yaml

@@ -135,6 +135,9 @@ importers:
       vite:
         specifier: ^7.0.0
         version: 7.0.6(@types/node@24.1.0)(jiti@2.5.1)(lightningcss@1.30.1)(tsx@4.20.3)(yaml@2.8.0)
+      vite-plugin-iframe-communicator:
+        specifier: ^0.0.1
+        version: 0.0.1(vite@7.0.6(@types/node@24.1.0)(jiti@2.5.1)(lightningcss@1.30.1)(tsx@4.20.3)(yaml@2.8.0))
       vite-progress-tracking-plugin:
         specifier: ^0.0.2
         version: 0.0.2(vite@7.0.6(@types/node@24.1.0)(jiti@2.5.1)(lightningcss@1.30.1)(tsx@4.20.3)(yaml@2.8.0))
@@ -2006,6 +2009,11 @@ packages:
     resolution: {integrity: sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==}
     engines: {node: '>= 0.8'}
 
+  vite-plugin-iframe-communicator@0.0.1:
+    resolution: {integrity: sha512-pAdbZWmC2oOlLYVCqZ0pmQox2iFh8iwwymsLzXuB5Un6BrLkLzTAmPNKWLiuUIXpBj6wi+8RlZHqDF7LpSvhlw==}
+    peerDependencies:
+      vite: ^7.0.0
+
   vite-progress-tracking-plugin@0.0.2:
     resolution: {integrity: sha512-tm5KIwicitTSw+7EVZT3rJ7gLkv7LNloFAxUhJRYCvZ3jQh85l3/GYHHMwl2HbuAaPDYEQOUB2w5IM3fC/a12w==}
     peerDependencies:
@@ -3885,6 +3893,10 @@ snapshots:
 
   vary@1.1.2: {}
 
+  vite-plugin-iframe-communicator@0.0.1(vite@7.0.6(@types/node@24.1.0)(jiti@2.5.1)(lightningcss@1.30.1)(tsx@4.20.3)(yaml@2.8.0)):
+    dependencies:
+      vite: 7.0.6(@types/node@24.1.0)(jiti@2.5.1)(lightningcss@1.30.1)(tsx@4.20.3)(yaml@2.8.0)
+
   vite-progress-tracking-plugin@0.0.2(vite@7.0.6(@types/node@24.1.0)(jiti@2.5.1)(lightningcss@1.30.1)(tsx@4.20.3)(yaml@2.8.0)):
     dependencies:
       vite: 7.0.6(@types/node@24.1.0)(jiti@2.5.1)(lightningcss@1.30.1)(tsx@4.20.3)(yaml@2.8.0)

+ 47 - 46
server.js

@@ -83,52 +83,6 @@ if (!isProduction) {
   console.log('Vite 开发服务器初始化完成');
 }
 
-// 生产环境:启动时加载 API 路由
-if (isProduction) {
-  console.log('生产环境: 加载编译后的 API 路由...');
-  const api = (await import('./dist/api/api.js')).default;
-  app.route('/', api);
-  console.log('API 路由加载完成');
-}
-// 开发环境:不在此处加载 API 路由,改为在中间件中动态加载
-
-// 添加动态 API 路由中间件
-app.use(async (c, next) => {
-  try {
-    // 开发环境:每次请求动态加载 API 路由
-    if (!isProduction && vite) {
-      // 动态加载最新 API 模块
-      const apiModule = await vite.ssrLoadModule('./src/server/api.ts');
-      
-      // 创建临时子应用并挂载路由
-      const apiApp = new Hono();
-      apiApp.route('/', apiModule.default);
-      
-      // 检查是否为 API 请求
-      if (
-        c.req.path.startsWith('/api')
-        || c.req.path.startsWith('/ui')
-        || c.req.path.startsWith('/doc')
-      ) {
-        // 直接由子应用处理 API 请求
-        return apiApp.fetch(c.req.raw, {
-          ...c.env,
-          // 传递原始请求对象
-          incoming: c.env.incoming,
-          outgoing: c.env.outgoing
-        });
-      }
-    }
-
-    await next();
-  } catch (e) {
-    if (!isProduction && vite) {
-      vite.ssrFixStacktrace(e);
-    }
-    console.error('API 路由加载错误:', e.stack);
-    return c.text('API 服务器错误', 500);
-  }
-});
 
 // 请求处理中间件 - 通用逻辑
 app.use(async (c, next) => {
@@ -187,6 +141,53 @@ app.use(async (c, next) => {
   }
 });
 
+// 生产环境:启动时加载 API 路由
+if (isProduction) {
+  console.log('生产环境: 加载编译后的 API 路由...');
+  const api = (await import('./dist/api/api.js')).default;
+  app.route('/', api);
+  console.log('API 路由加载完成');
+}
+// 开发环境:不在此处加载 API 路由,改为在中间件中动态加载
+
+// 添加动态 API 路由中间件
+app.use(async (c, next) => {
+  try {
+    // 开发环境:每次请求动态加载 API 路由
+    if (!isProduction && vite) {
+      // 动态加载最新 API 模块
+      const apiModule = await vite.ssrLoadModule('./src/server/api.ts');
+      
+      // 创建临时子应用并挂载路由
+      const apiApp = new Hono();
+      apiApp.route('/', apiModule.default);
+      
+      // 检查是否为 API 请求
+      if (
+        c.req.path.startsWith('/api')
+        || c.req.path.startsWith('/ui')
+        || c.req.path.startsWith('/doc')
+      ) {
+        // 直接由子应用处理 API 请求
+        return apiApp.fetch(c.req.raw, {
+          ...c.env,
+          // 传递原始请求对象
+          incoming: c.env.incoming,
+          outgoing: c.env.outgoing
+        });
+      }
+    }
+
+    await next();
+  } catch (e) {
+    if (!isProduction && vite) {
+      vite.ssrFixStacktrace(e);
+    }
+    console.error('API 路由加载错误:', e.stack);
+    return c.text('API 服务器错误', 500);
+  }
+});
+
 // 请求处理中间件 - SSR 渲染逻辑
 app.use(async (c) => {
   try {