Compare commits

..

No commits in common. "main" and "v0.2.0" have entirely different histories.
main ... v0.2.0

3 changed files with 10 additions and 8 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 {
http.Handle("/_livereloader_", reloader{ mux.Handle("/_livereloader_", reloader{
clientVersion: options.ClientVersion, clientVersion: options.ClientVersion,
}) })
} }

View File

@ -2,7 +2,6 @@ package spa_handler
import ( import (
"bytes" "bytes"
"fmt"
"io" "io"
"io/fs" "io/fs"
"net/http" "net/http"
@ -17,11 +16,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
} }
merge := bytes.NewBuffer([]byte{}) modified := []byte{}
for _, a := range appender { for _, a := range appender {
merge.WriteString(a) modified = append(modified, []byte(a)...)
} }
newContent := strings.Replace(string(content), "</html>", fmt.Sprintf("%s\n</html>", merge.String()), 1) modified = append(modified, content...)
stats, statErr := file.Stat() stats, statErr := file.Stat()
if statErr != nil { if statErr != nil {
@ -34,7 +33,7 @@ func tryToDeliverFile(file fs.File, path string, w http.ResponseWriter, r *http.
} }
contentType = getContentTypeDetail(contentType, stats) contentType = getContentTypeDetail(contentType, stats)
fileSeeker := bytes.NewReader([]byte(newContent)) fileSeeker := bytes.NewReader(modified)
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,8 +38,11 @@ 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)
}; };
} }
spaHandlerLiveReloaderConnect(); document.onload = function() {
spaHandlerLiveReloaderConnect();
};
</script> </script>
<!-- [spa_handler] Live Reloading Script End -->
`, clientVersion) `, clientVersion)
} }