浏览代码

修复MIME类型错误:静态资源404直接返回 + 支持自定义base路径

cnbugs 1 周之前
父节点
当前提交
c453d3425d
共有 2 个文件被更改,包括 14 次插入0 次删除
  1. 13 0
      backend/main.go
  2. 1 0
      frontend/vite.config.ts

+ 13 - 0
backend/main.go

@@ -5,7 +5,9 @@ import (
 	"auto-ssl/handlers"
 	"auto-ssl/services"
 	"log"
+	"net/http"
 	"os"
+	"strings"
 	"time"
 
 	"github.com/gin-contrib/cors"
@@ -57,6 +59,17 @@ func main() {
 	}
 
 	r.NoRoute(func(c *gin.Context) {
+		// 静态资源请求直接返回404,避免返回html导致MIME类型错误
+		if strings.HasPrefix(c.Request.URL.Path, "/assets/") || 
+		   strings.HasSuffix(c.Request.URL.Path, ".js") || 
+		   strings.HasSuffix(c.Request.URL.Path, ".css") ||
+		   strings.HasSuffix(c.Request.URL.Path, ".png") ||
+		   strings.HasSuffix(c.Request.URL.Path, ".svg") ||
+		   strings.HasSuffix(c.Request.URL.Path, ".ico") {
+			c.AbortWithStatus(http.StatusNotFound)
+			return
+		}
+		// 其他路由返回index.html给前端处理
 		c.File("./dist/index.html")
 	})
 

+ 1 - 0
frontend/vite.config.ts

@@ -4,4 +4,5 @@ import vue from '@vitejs/plugin-vue'
 // https://vite.dev/config/
 export default defineConfig({
   plugins: [vue()],
+  base: process.env.VITE_BASE_URL || '/',
 })