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,
})
if options.ActivateLiveReloading {
http.Handle("/_livereloader_", reloader{
mux.Handle("/_livereloader_", reloader{
clientVersion: options.ClientVersion,
})
}

View File

@ -2,7 +2,6 @@ package spa_handler
import (
"bytes"
"fmt"
"io"
"io/fs"
"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)
return
}
merge := bytes.NewBuffer([]byte{})
modified := []byte{}
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()
if statErr != nil {
@ -34,7 +33,7 @@ func tryToDeliverFile(file fs.File, path string, w http.ResponseWriter, r *http.
}
contentType = getContentTypeDetail(contentType, stats)
fileSeeker := bytes.NewReader([]byte(newContent))
fileSeeker := bytes.NewReader(modified)
w.Header().Set("Content-Type", contentType)
http.ServeContent(w, r, path, time.Now(), fileSeeker)
}

View File

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