livereload

This commit is contained in:
Ryan Stafford 2023-07-26 16:53:06 -04:00
parent 1858f1aec0
commit 3fe31bd5b3
5 changed files with 41 additions and 1 deletions

View file

@ -9,7 +9,7 @@ dev:
$(MAKE) -j2 --no-print-directory reload serve $(MAKE) -j2 --no-print-directory reload serve
reload: reload:
websocketd --loglevel=fatal --port=8009 watchexec -e html,css,js -d 500 'echo "$$WATCHEXEC_WRITTEN_PATH"' websocketd --loglevel=fatal --port=8009 watchexec --no-vcs-ignore -e html,css,js 'echo "$$WATCHEXEC_WRITTEN_PATH"'
VERSION: VERSION:
git describe --tag > $@ git describe --tag > $@

33
public/ws.js Normal file
View file

@ -0,0 +1,33 @@
var ok = false
var t = 800
var ws
var prot = location.protocol == 'https:' ? "wss://":"ws://"
let port = ":8009"
function start(){
let url = prot + document.location.hostname + port + document.location.pathname + document.location.search;
console.log('connecting to ', url);
ws = new WebSocket(url);
ws.onopen = function(){
console.log("open");
t = 800
}
ws.onmessage = function(msg){
console.log("reload:", msg.data)
if (msg.data == "") {
return
}
window.location.reload()
}
ws.onclose = function(){
console.log("close");
setTimeout(function(){
//start()
if (t < 10 * 1000) t += 200
}, t);
};
}
console.log("ws");
if (typeof WebSocket != 'undefined') {
start()
}

View file

@ -215,6 +215,9 @@ func Initialize(Host string, r *http.Request) (State, error) {
Status: http.StatusOK, Status: http.StatusOK,
Version: version, Version: version,
} }
if watch != nil {
state.Watch = *watch
}
lemmyDomain := os.Getenv("LEMMY_DOMAIN") lemmyDomain := os.Getenv("LEMMY_DOMAIN")
if lemmyDomain != "" { if lemmyDomain != "" {
state.Host = "." state.Host = "."

View file

@ -65,6 +65,7 @@ type Session struct {
} }
type State struct { type State struct {
Watch bool
Version string Version string
Client *lemmy.Client Client *lemmy.Client
HTTPClient *http.Client HTTPClient *http.Client

View file

@ -141,6 +141,9 @@
{{ end }} {{ end }}
<script src="/_/static/utils.js?v=22"></script> <script src="/_/static/utils.js?v=22"></script>
{{ if .Watch }}
<script src="/_/static/ws.js"></script>
{{ end }}
{{ template "sidebar.html" . }} {{ template "sidebar.html" . }}
</main> </main>
{{ end }} {{ end }}