Merge pull request #2261 from wzxjohn/hotfix/analytic

fix: root page does not have analytic code
This commit is contained in:
Seefs
2025-11-24 14:06:02 +08:00
committed by GitHub

View File

@@ -4,6 +4,7 @@ import (
"embed" "embed"
"io/fs" "io/fs"
"net/http" "net/http"
"os"
"github.com/gin-contrib/static" "github.com/gin-contrib/static"
) )
@@ -14,7 +15,7 @@ type embedFileSystem struct {
http.FileSystem http.FileSystem
} }
func (e embedFileSystem) Exists(prefix string, path string) bool { func (e *embedFileSystem) Exists(prefix string, path string) bool {
_, err := e.Open(path) _, err := e.Open(path)
if err != nil { if err != nil {
return false return false
@@ -22,12 +23,21 @@ func (e embedFileSystem) Exists(prefix string, path string) bool {
return true return true
} }
func (e *embedFileSystem) Open(name string) (http.File, error) {
if name == "/" {
// This will make sure the index page goes to NoRouter handler,
// which will use the replaced index bytes with analytic codes.
return nil, os.ErrNotExist
}
return e.FileSystem.Open(name)
}
func EmbedFolder(fsEmbed embed.FS, targetPath string) static.ServeFileSystem { func EmbedFolder(fsEmbed embed.FS, targetPath string) static.ServeFileSystem {
efs, err := fs.Sub(fsEmbed, targetPath) efs, err := fs.Sub(fsEmbed, targetPath)
if err != nil { if err != nil {
panic(err) panic(err)
} }
return embedFileSystem{ return &embedFileSystem{
FileSystem: http.FS(efs), FileSystem: http.FS(efs),
} }
} }