Skip to content

Commit 7a98a48

Browse files
committed
Handle case w/o html extension
1 parent 50c72ba commit 7a98a48

File tree

2 files changed

+58
-23
lines changed

2 files changed

+58
-23
lines changed

nextrouter/nextrouter.go

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,17 @@ import (
44
"fmt"
55
"io/fs"
66
"net/http"
7+
"path/filepath"
8+
"strings"
79

810
"github.com/go-chi/chi/v5"
911
)
1012

11-
func serveFile(router chi.Router, fileSystem fs.FS, fileName string) {
12-
13-
handler := func(w http.ResponseWriter, r *http.Request) {
14-
fmt.Println("Requesting file: " + fileName)
15-
bytes, err := fs.ReadFile(fileSystem, fileName)
16-
17-
if err != nil {
18-
http.Error(w, http.StatusText(404), 404)
19-
}
20-
_, err = w.Write(bytes)
21-
22-
if err != nil {
23-
http.Error(w, http.StatusText(404), 404)
24-
}
25-
}
26-
27-
router.Get("/"+fileName, handler)
13+
// Handler returns an HTTP handler for serving a next-based static site
14+
func Handler(fileSystem fs.FS) (http.Handler, error) {
15+
rtr := chi.NewRouter()
16+
buildRouter(rtr, fileSystem, "")
17+
return rtr, nil
2818
}
2919

3020
func buildRouter(rtr chi.Router, fileSystem fs.FS, name string) {
@@ -54,9 +44,35 @@ func buildRouter(rtr chi.Router, fileSystem fs.FS, name string) {
5444
}
5545
}
5646

57-
// Handler returns an HTTP handler for serving a next-based static site
58-
func Handler(fileSystem fs.FS) (http.Handler, error) {
59-
rtr := chi.NewRouter()
60-
buildRouter(rtr, fileSystem, "")
61-
return rtr, nil
47+
func serveFile(router chi.Router, fileSystem fs.FS, fileName string) {
48+
49+
// We only handle .html files for now
50+
ext := filepath.Ext(fileName)
51+
if ext != ".html" {
52+
return
53+
}
54+
55+
fmt.Println("Requesting file: " + fileName)
56+
bytes, err := fs.ReadFile(fileSystem, fileName)
57+
58+
handler := func(w http.ResponseWriter, r *http.Request) {
59+
60+
if err != nil {
61+
http.Error(w, http.StatusText(404), 404)
62+
}
63+
_, err = w.Write(bytes)
64+
65+
if err != nil {
66+
http.Error(w, http.StatusText(404), 404)
67+
}
68+
}
69+
70+
fileNameWithoutExtension := removeFileExtension(fileName)
71+
72+
router.Get("/"+fileName, handler)
73+
router.Get("/"+fileNameWithoutExtension, handler)
74+
}
75+
76+
func removeFileExtension(fileName string) string {
77+
return strings.TrimSuffix(fileName, filepath.Ext(fileName))
6278
}

nextrouter/nextrouter_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ func TestNextRouter(t *testing.T) {
3131

3232
t.Run("Serves file at root", func(t *testing.T) {
3333
t.Parallel()
34-
3534
rootFS := memfs.New()
3635
err := rootFS.WriteFile("test.html", []byte("test123"), 0755)
3736
require.NoError(t, err)
@@ -50,6 +49,26 @@ func TestNextRouter(t *testing.T) {
5049
require.Equal(t, res.StatusCode, 200)
5150
})
5251

52+
t.Run("Serves html file without extension", func(t *testing.T) {
53+
t.Parallel()
54+
rootFS := memfs.New()
55+
err := rootFS.WriteFile("test.html", []byte("test-no-extension"), 0755)
56+
require.NoError(t, err)
57+
58+
router, err := nextrouter.Handler(rootFS)
59+
require.NoError(t, err)
60+
server := httptest.NewServer(router)
61+
62+
res, err := request(server, "/test")
63+
require.NoError(t, err)
64+
defer res.Body.Close()
65+
66+
body, err := io.ReadAll(res.Body)
67+
require.NoError(t, err)
68+
require.Equal(t, string(body), "test-no-extension")
69+
require.Equal(t, res.StatusCode, 200)
70+
})
71+
5372
t.Run("Serves nested file", func(t *testing.T) {
5473
t.Parallel()
5574

0 commit comments

Comments
 (0)