Fix: Use StaticFS for assets to serve correct MIME types

This commit is contained in:
小萝
2026-04-29 07:53:49 +00:00
parent aaccef6bf1
commit 6ddb8f58ad
+10 -7
View File
@@ -1,6 +1,8 @@
package routes package routes
import ( import (
"net/http"
"sale/internal/api/handlers" "sale/internal/api/handlers"
"sale/internal/api/middlewares" "sale/internal/api/middlewares"
"sale/internal/models" "sale/internal/models"
@@ -206,20 +208,21 @@ func SetupRoutes(r *gin.Engine) {
// Serve uploads // Serve uploads
r.Static("/uploads", "./uploads") r.Static("/uploads", "./uploads")
// Serve frontend static assets (CSS, JS, images, etc.)
r.StaticFS("/assets", http.Dir("./static/assets"))
// SPA fallback - serve index.html for all other routes (Vue Router History Mode) // SPA fallback - serve index.html for all other routes (Vue Router History Mode)
// This must be the last route to catch all unmatched paths // This must be the last route to catch all unmatched paths
r.NoRoute(func(c *gin.Context) { r.NoRoute(func(c *gin.Context) {
path := c.Request.URL.Path path := c.Request.URL.Path
// Serve index.html for all non-API routes (Vue Router History Mode) // For root and SPA routes, serve index.html
// But skip API, assets, and uploads paths // API requests will return 404 if not found
if path == "/" || path == "/favicon.ico" || path == "/icons.svg" || path == "/favicon.svg" || if path == "/" || path == "/favicon.ico" || path == "/icons.svg" || path == "/favicon.svg" ||
len(path) > 7 && path[:7] == "/assets/" || len(path) > 9 && path[:9] == "/uploads/" || len(path) > 4 && path[:4] != "/api/" {
len(path) > 4 && path[:4] == "/api/" {
// For root and special paths, serve index.html directly
c.File("/app/static/index.html") c.File("/app/static/index.html")
} else { } else {
// For all other routes, serve index.html (Vue Router handles client-side routing) // API routes - let them return 404 if not found
c.File("/app/static/index.html") c.AbortWithStatus(404)
} }
}) })
} }