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
import (
"net/http"
"sale/internal/api/handlers"
"sale/internal/api/middlewares"
"sale/internal/models"
@@ -206,20 +208,21 @@ func SetupRoutes(r *gin.Engine) {
// Serve 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)
// This must be the last route to catch all unmatched paths
r.NoRoute(func(c *gin.Context) {
path := c.Request.URL.Path
// Serve index.html for all non-API routes (Vue Router History Mode)
// But skip API, assets, and uploads paths
// For root and SPA routes, serve index.html
// API requests will return 404 if not found
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/" {
// For root and special paths, serve index.html directly
len(path) > 4 && path[:4] != "/api/" {
c.File("/app/static/index.html")
} else {
// For all other routes, serve index.html (Vue Router handles client-side routing)
c.File("/app/static/index.html")
// API routes - let them return 404 if not found
c.AbortWithStatus(404)
}
})
}