Skip to content

Commit 2ef34e0

Browse files
committed
Fix leaflet and hlsjs js files not being served
1 parent 1bca07e commit 2ef34e0

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

Diff for: httpFs.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ func (a *goBlog) serveFs(f fs.FS, basePath string) http.HandlerFunc {
2121
switch path.Ext(fileName) {
2222
case ".js":
2323
w.Header().Set(contentType, contenttype.JSUTF8)
24-
_ = a.min.Get().Minify(contenttype.JS, w, file)
24+
// Can't minify, because minify throws errors for some JS files
25+
_, _ = io.Copy(w, file)
2526
case ".css":
2627
w.Header().Set(contentType, contenttype.CSSUTF8)
2728
_ = a.min.Get().Minify(contenttype.CSS, w, file)

Diff for: httpFs_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"io"
6+
"io/fs"
7+
"net/http"
8+
"net/http/httptest"
9+
"testing"
10+
11+
"github.com/carlmjohnson/requests"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func Test_httpFs(t *testing.T) {
16+
app := &goBlog{
17+
cfg: createDefaultTestConfig(t),
18+
}
19+
_ = app.initConfig(false)
20+
21+
t.Run("Leaflet", func(t *testing.T) {
22+
t.Parallel()
23+
testFs(t, app, leafletFiles, "/-/", []string{
24+
"/-/leaflet/leaflet.js",
25+
"/-/leaflet/leaflet.css",
26+
"/-/leaflet/markercluster.js",
27+
"/-/leaflet/markercluster.css",
28+
"/-/leaflet/markercluster.default.css",
29+
})
30+
})
31+
32+
t.Run("Hls.js", func(t *testing.T) {
33+
t.Parallel()
34+
testFs(t, app, hlsjsFiles, "/-/", []string{
35+
"/-/hlsjs/hls.js",
36+
})
37+
})
38+
39+
}
40+
41+
func testFs(t *testing.T, app *goBlog, files fs.FS, prefix string, paths []string) {
42+
handler := app.serveFs(files, prefix)
43+
44+
for _, fp := range paths {
45+
t.Run(fp, func(t *testing.T) {
46+
fp := fp
47+
48+
t.Parallel()
49+
50+
w := httptest.NewRecorder()
51+
r, _ := requests.URL(fp).Method(http.MethodGet).Request(context.Background())
52+
53+
handler.ServeHTTP(w, r)
54+
55+
result := w.Result()
56+
bodyContent, _ := io.ReadAll(result.Body)
57+
result.Body.Close()
58+
59+
require.NotEmpty(t, bodyContent)
60+
})
61+
}
62+
}

0 commit comments

Comments
 (0)