[优化] 更新缓存控制逻辑,增加静态资源和文档请求的处理

This commit is contained in:
ryan
2026-03-13 13:34:04 +08:00
parent dbecf690f5
commit 55d1a3f2c8
3 changed files with 145 additions and 1 deletions
+24
View File
@@ -22,6 +22,7 @@ func setWebRouter(router *gin.Engine, buildFS embed.FS, indexPage []byte) {
router.Use(middleware.GlobalWebRateLimit())
fileDownloadRoute := router.Group("/")
fileDownloadRoute.GET("/upload/:file", middleware.DownloadRateLimit(), controller.DownloadFile)
router.Use(normalizeStaticExportDataNavigation())
router.Use(middleware.Cache())
router.Use(static.Serve("/", common.EmbedFolder(buildFS, "web/build")))
router.NoRoute(func(c *gin.Context) {
@@ -60,6 +61,29 @@ func serveExportedPage(c *gin.Context, buildFS fs.FS) bool {
return false
}
func normalizeStaticExportDataNavigation() gin.HandlerFunc {
return func(c *gin.Context) {
requestPath := c.Request.URL.Path
if strings.HasSuffix(requestPath, ".txt") && isDocumentNavigationRequest(c.Request) {
normalizedPath := strings.TrimSuffix(requestPath, ".txt")
if normalizedPath == "" {
normalizedPath = "/"
}
c.Request.URL.Path = normalizedPath
}
c.Next()
}
}
func isDocumentNavigationRequest(request *http.Request) bool {
if request.Header.Get("Sec-Fetch-Mode") == "navigate" || request.Header.Get("Sec-Fetch-Dest") == "document" {
return true
}
return strings.Contains(request.Header.Get("Accept"), "text/html")
}
func isStaticAssetRequest(requestPath string) bool {
return strings.HasPrefix(requestPath, "/_next/") || pathpkg.Ext(requestPath) != ""
}