Skip to content

Commit 36eb6b5

Browse files
committed
fix(middleware): fallback to html5 route file
1 parent dac56bc commit 36eb6b5

4 files changed

Lines changed: 40 additions & 0 deletions

File tree

middleware/static.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,20 @@ func (config StaticConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
242242
if !isIgnorableOpenFileError(err) {
243243
return err
244244
}
245+
246+
if config.HTML5 {
247+
html5File, html5Err := currentFS.Open(filePath + ".html")
248+
if html5Err == nil {
249+
defer html5File.Close()
250+
251+
html5Info, err := html5File.Stat()
252+
if err != nil {
253+
return err
254+
}
255+
256+
return serveFile(c, html5File, html5Info)
257+
}
258+
}
245259
// file with that path did not exist, so we continue down in middleware/handler chain, hoping that we end up in
246260
// handler that is meant to handle this request
247261
err = next(c)
@@ -270,6 +284,20 @@ func (config StaticConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
270284
if info.IsDir() {
271285
index, err := currentFS.Open(path.Join(filePath, config.Index))
272286
if err != nil {
287+
if config.HTML5 && !config.Browse {
288+
html5File, html5Err := currentFS.Open(filePath + ".html")
289+
if html5Err == nil {
290+
defer html5File.Close()
291+
292+
info, err = html5File.Stat()
293+
if err != nil {
294+
return err
295+
}
296+
297+
return serveFile(c, html5File, info)
298+
}
299+
}
300+
273301
if config.Browse {
274302
return listDir(dirListTemplate, filePath, currentFS, c.Response())
275303
}

middleware/static_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ func TestStatic(t *testing.T) {
7777
expectCode: http.StatusOK,
7878
expectContains: "<h1>Hello from index</h1>",
7979
},
80+
{
81+
name: "ok, when html5 mode serve route html next to a directory",
82+
givenConfig: &StaticConfig{
83+
Root: "testdata/dist/public",
84+
HTML5: true,
85+
},
86+
whenURL: "/camera",
87+
expectCode: http.StatusOK,
88+
expectContains: "<h1>Camera page</h1>",
89+
},
8090
{
8191
name: "ok, serve index as directory index listing files directory",
8292
givenConfig: &StaticConfig{
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Camera page</h1>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
camera route directory

0 commit comments

Comments
 (0)