# 静态文件 MIME 类型问题修复报告 **完成时间**: 2026-03-24 **问题等级**: 🔴 P0 - 阻塞性问题 **修复状态**: ✅ **已完成** --- ## 🐛 **问题描述** ### 错误现象 浏览器控制台报错: ```javascript Failed to load module script: Expected a JavaScript-or-Wasm module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec. ``` ```javascript Uncaught (in promise) TypeError: Failed to fetch dynamically imported module: http://localhost:9531/assets/Dashboard-BMrerBTn.js ``` ### 影响 - ❌ 前端页面无法加载 - ❌ 所有 JS 文件被当作 HTML 返回 - ❌ Vue Router 无法工作 - ❌ 用户完全无法使用系统 --- ## 🔍 **问题分析** ### 根本原因 **Gin 路由注册顺序错误**: ```go // ❌ 错误的顺序 s.engine.NoRoute(func(c *gin.Context) { // 处理所有未匹配的路由 // 包括 /assets/*.js, /assets/*.css }) s.engine.StaticFS("/assets", httpFS) // ← 这个永远不会被执行! ``` **问题机制**: 1. Gin 按注册顺序匹配路由 2. `NoRoute` 是兜底路由,优先级最高 3. 所有未匹配的路由都被 `NoRoute` 拦截 4. `StaticFS` 注册在 `NoRoute` 之后,永远不会被调用 5. `/assets/*.js` 请求进入 `NoRoute` 处理器 6. `NoRoute` 尝试从 embed FS 读取文件失败 7. 回退到返回 `index.html`(用于 SPA 路由) 8. JS 文件内容变成了 HTML,导致 MIME 类型错误 --- ### 日志证据 **错误的请求日志**: ```json { "level": "info", "method": "GET", "path": "/assets/index-CMxu2vHG.js", "status": 200, "Content-Type": "text/html" // ← 应该是 application/javascript } ``` **正确的请求日志**: ```json { "level": "info", "method": "GET", "path": "/assets/index-CMxu2vHG.js", "status": 200, "Content-Type": "application/javascript; charset=utf-8" // ✅ 正确 } ``` --- ## ✅ **解决方案** ### 修复方法 **调整路由注册顺序**: ```go // ✅ 正确的顺序 if staticFS != nil { httpFS := http.FS(staticFS) // 1️⃣ 先注册静态文件目录(高优先级) s.engine.StaticFS("/assets", httpFS) s.engine.StaticFS("/static", httpFS) // 2️⃣ 再注册 NoRoute 处理 SPA 路由(低优先级) s.engine.NoRoute(func(c *gin.Context) { path := c.Request.URL.Path // API 请求返回 404 if strings.HasPrefix(path, "/api/") { c.JSON(404, gin.H{"error": "API not found"}) return } // 尝试直接提供文件 filePath := strings.TrimPrefix(path, "/") if filePath == "" { filePath = "index.html" } file, err := staticFS.Open(filePath) if err == nil { defer file.Close() content, _ := io.ReadAll(file) c.Data(200, getContentType(filePath), content) return } // 回退到 index.html(Vue Router 需要) file, _ = staticFS.Open("index.html") if file != nil { defer file.Close() content, _ := io.ReadAll(file) c.Data(200, "text/html; charset=utf-8", content) } }) } ``` --- ### 修改的文件 **文件**: [`internal/api/server.go`](file://e:\Project\MeshRay\internal\api\server.go#L117-L160) **修改内容**: - Line 121-122: 移动 `StaticFS` 注册到 `NoRoute` 之前 - Line 155-156: 从 `NoRoute` 之后移到之前 **代码对比**: ```diff if staticFS != nil { httpFS := http.FS(staticFS) - // 注册静态文件路由 - s.engine.NoRoute(...) - - // 注册静态文件目录 + // 先注册静态文件目录(优先级高) s.engine.StaticFS("/assets", httpFS) s.engine.StaticFS("/static", httpFS) + + // 再注册 NoRoute 处理 SPA 路由(优先级低) + s.engine.NoRoute(...) } ``` --- ## 🧪 **验证结果** ### 1. 编译测试 ```bash cd e:\Project\MeshRay go build -o meshray-test.exe ./cmd/meshray # ✅ 编译成功,无错误 ``` --- ### 2. 启动服务 ```bash ./meshray-test.exe # 日志显示: # {"level":"info","message":"使用内嵌的静态文件"} # {"level":"info","message":"Starting MeshRay","address":":9531"} # ✅ 服务启动成功 ``` --- ### 3. MIME 类型验证 **测试 JS 文件**: ```bash curl -I http://localhost:9531/assets/index-CMxu2vHG.js # 响应头: HTTP/1.1 200 OK Content-Type: application/javascript; charset=utf-8 # ✅ 正确 ``` **测试 CSS 文件**: ```bash curl -I http://localhost:9531/assets/index-JLRO43JB.css # 响应头: HTTP/1.1 200 OK Content-Type: text/css; charset=utf-8 # ✅ 正确 ``` **测试 HTML 页面**: ```bash curl http://localhost:9531 # 响应: