From 3fe31bd5b3760208b112feb0c5119b4f1018aa56 Mon Sep 17 00:00:00 2001 From: Ryan Stafford Date: Wed, 26 Jul 2023 16:53:06 -0400 Subject: [PATCH] livereload --- Makefile | 2 +- public/ws.js | 33 +++++++++++++++++++++++++++++++++ routes.go | 3 +++ state.go | 1 + templates/main.html | 3 +++ 5 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 public/ws.js diff --git a/Makefile b/Makefile index 701866f..a7e0a0c 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ dev: $(MAKE) -j2 --no-print-directory reload serve 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: git describe --tag > $@ diff --git a/public/ws.js b/public/ws.js new file mode 100644 index 0000000..08ac7c3 --- /dev/null +++ b/public/ws.js @@ -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() +} diff --git a/routes.go b/routes.go index 5bd0c32..e8af5f0 100644 --- a/routes.go +++ b/routes.go @@ -215,6 +215,9 @@ func Initialize(Host string, r *http.Request) (State, error) { Status: http.StatusOK, Version: version, } + if watch != nil { + state.Watch = *watch + } lemmyDomain := os.Getenv("LEMMY_DOMAIN") if lemmyDomain != "" { state.Host = "." diff --git a/state.go b/state.go index 474d893..181417f 100644 --- a/state.go +++ b/state.go @@ -65,6 +65,7 @@ type Session struct { } type State struct { + Watch bool Version string Client *lemmy.Client HTTPClient *http.Client diff --git a/templates/main.html b/templates/main.html index 053b4ce..a421bcb 100644 --- a/templates/main.html +++ b/templates/main.html @@ -141,6 +141,9 @@ {{ end }} + {{ if .Watch }} + + {{ end }} {{ template "sidebar.html" . }} {{ end }}