From 36eb6b55f3d538c6dcecad5b2e636769e773110a Mon Sep 17 00:00:00 2001 From: Emmanuel Ortiz Date: Thu, 4 Jun 2026 01:34:35 -0600 Subject: [PATCH] fix(middleware): fallback to html5 route file --- middleware/static.go | 28 +++++++++++++++++++ middleware/static_test.go | 10 +++++++ middleware/testdata/dist/public/camera.html | 1 + .../testdata/dist/public/camera/route.txt | 1 + 4 files changed, 40 insertions(+) create mode 100644 middleware/testdata/dist/public/camera.html create mode 100644 middleware/testdata/dist/public/camera/route.txt diff --git a/middleware/static.go b/middleware/static.go index a9e67b626..c8842ae71 100644 --- a/middleware/static.go +++ b/middleware/static.go @@ -242,6 +242,20 @@ func (config StaticConfig) ToMiddleware() (echo.MiddlewareFunc, error) { if !isIgnorableOpenFileError(err) { return err } + + if config.HTML5 { + html5File, html5Err := currentFS.Open(filePath + ".html") + if html5Err == nil { + defer html5File.Close() + + html5Info, err := html5File.Stat() + if err != nil { + return err + } + + return serveFile(c, html5File, html5Info) + } + } // file with that path did not exist, so we continue down in middleware/handler chain, hoping that we end up in // handler that is meant to handle this request err = next(c) @@ -270,6 +284,20 @@ func (config StaticConfig) ToMiddleware() (echo.MiddlewareFunc, error) { if info.IsDir() { index, err := currentFS.Open(path.Join(filePath, config.Index)) if err != nil { + if config.HTML5 && !config.Browse { + html5File, html5Err := currentFS.Open(filePath + ".html") + if html5Err == nil { + defer html5File.Close() + + info, err = html5File.Stat() + if err != nil { + return err + } + + return serveFile(c, html5File, info) + } + } + if config.Browse { return listDir(dirListTemplate, filePath, currentFS, c.Response()) } diff --git a/middleware/static_test.go b/middleware/static_test.go index c31e1a4b9..1d27c33a7 100644 --- a/middleware/static_test.go +++ b/middleware/static_test.go @@ -77,6 +77,16 @@ func TestStatic(t *testing.T) { expectCode: http.StatusOK, expectContains: "

Hello from index

", }, + { + name: "ok, when html5 mode serve route html next to a directory", + givenConfig: &StaticConfig{ + Root: "testdata/dist/public", + HTML5: true, + }, + whenURL: "/camera", + expectCode: http.StatusOK, + expectContains: "

Camera page

", + }, { name: "ok, serve index as directory index listing files directory", givenConfig: &StaticConfig{ diff --git a/middleware/testdata/dist/public/camera.html b/middleware/testdata/dist/public/camera.html new file mode 100644 index 000000000..8088c9469 --- /dev/null +++ b/middleware/testdata/dist/public/camera.html @@ -0,0 +1 @@ +

Camera page

diff --git a/middleware/testdata/dist/public/camera/route.txt b/middleware/testdata/dist/public/camera/route.txt new file mode 100644 index 000000000..4e0c80760 --- /dev/null +++ b/middleware/testdata/dist/public/camera/route.txt @@ -0,0 +1 @@ +camera route directory