Compare commits

...

1 Commits
v0.2.0 ... main

Author SHA1 Message Date
c6ab3e2466 fix not working stuff 2025-06-06 21:06:38 +02:00
3 changed files with 8 additions and 10 deletions

View File

@ -57,7 +57,7 @@ func HandleFiles(path string, options HandlerOptions, mux *http.ServeMux) {
clientVersion: options.ClientVersion, clientVersion: options.ClientVersion,
}) })
if options.ActivateLiveReloading { if options.ActivateLiveReloading {
mux.Handle("/_livereloader_", reloader{ http.Handle("/_livereloader_", reloader{
clientVersion: options.ClientVersion, clientVersion: options.ClientVersion,
}) })
} }

View File

@ -2,6 +2,7 @@ package spa_handler
import ( import (
"bytes" "bytes"
"fmt"
"io" "io"
"io/fs" "io/fs"
"net/http" "net/http"
@ -16,11 +17,11 @@ func tryToDeliverFile(file fs.File, path string, w http.ResponseWriter, r *http.
http.Error(w, "can not read file", http.StatusInternalServerError) http.Error(w, "can not read file", http.StatusInternalServerError)
return return
} }
modified := []byte{} merge := bytes.NewBuffer([]byte{})
for _, a := range appender { for _, a := range appender {
modified = append(modified, []byte(a)...) merge.WriteString(a)
} }
modified = append(modified, content...) newContent := strings.Replace(string(content), "</html>", fmt.Sprintf("%s\n</html>", merge.String()), 1)
stats, statErr := file.Stat() stats, statErr := file.Stat()
if statErr != nil { if statErr != nil {
@ -33,7 +34,7 @@ func tryToDeliverFile(file fs.File, path string, w http.ResponseWriter, r *http.
} }
contentType = getContentTypeDetail(contentType, stats) contentType = getContentTypeDetail(contentType, stats)
fileSeeker := bytes.NewReader(modified) fileSeeker := bytes.NewReader([]byte(newContent))
w.Header().Set("Content-Type", contentType) w.Header().Set("Content-Type", contentType)
http.ServeContent(w, r, path, time.Now(), fileSeeker) http.ServeContent(w, r, path, time.Now(), fileSeeker)
} }

View File

@ -9,8 +9,8 @@ import (
func clientScript(clientVersion string) string { func clientScript(clientVersion string) string {
return fmt.Sprintf(` return fmt.Sprintf(`
<!-- [spa_handler] Live Reloading Script Start -->
<script type="application/javascript"> <script type="application/javascript">
// [spa_handler] live reload script
function spaHandlerLiveReloaderConnect() { function spaHandlerLiveReloaderConnect() {
const ws = new WebSocket("/_livereloader_"); const ws = new WebSocket("/_livereloader_");
ws.onopen = function() { ws.onopen = function() {
@ -38,11 +38,8 @@ func clientScript(clientVersion string) string {
console.warning('[spa_handler]: something was wrong with websocket connection! ', err) console.warning('[spa_handler]: something was wrong with websocket connection! ', err)
}; };
} }
document.onload = function() { spaHandlerLiveReloaderConnect();
spaHandlerLiveReloaderConnect();
};
</script> </script>
<!-- [spa_handler] Live Reloading Script End -->
`, clientVersion) `, clientVersion)
} }