feat: add devmode flag; introduce live reload support; update examples
This commit is contained in:
@@ -1,54 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-via/via"
|
||||
"github.com/go-via/via/h"
|
||||
)
|
||||
|
||||
func LiveReloadPlugin(v *via.V) {
|
||||
v.HandleFunc("GET /dev/reload", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
|
||||
<-r.Context().Done()
|
||||
})
|
||||
v.AppendToFoot(h.Script(h.Raw(`
|
||||
if (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') {
|
||||
const evtSource = new EventSource('/dev/reload');
|
||||
let overlay = null;
|
||||
let showTimer = null;
|
||||
|
||||
evtSource.onerror = () => {
|
||||
evtSource.close();
|
||||
|
||||
showTimer = setTimeout(() => {
|
||||
if (!overlay) {
|
||||
overlay = document.createElement('div');
|
||||
overlay.style.cssText = 'position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background: rgba(200, 200, 200, 0.95); padding: 20px 40px; border-radius: 8px; color: #333; font-size: 24px; z-index: 999999; font-family: -apple-system, sans-serif;';
|
||||
overlay.textContent = '🔌 Reconnecting...';
|
||||
document.body.appendChild(overlay);
|
||||
}
|
||||
}, 1000);
|
||||
|
||||
(async function poll() {
|
||||
for (let i = 0; i < 100; i++) {
|
||||
try {
|
||||
const res = await fetch('/', { method: 'HEAD', signal: AbortSignal.timeout(1000) });
|
||||
if (res.ok) {
|
||||
clearTimeout(showTimer);
|
||||
if (overlay) overlay.remove();
|
||||
location.reload();
|
||||
return;
|
||||
}
|
||||
} catch (e) {}
|
||||
await new Promise(r => setTimeout(r, 50));
|
||||
}
|
||||
})();
|
||||
};
|
||||
}
|
||||
`)))
|
||||
}
|
||||
@@ -12,7 +12,8 @@ func main() {
|
||||
|
||||
v.Config(via.Options{
|
||||
DocumentTitle: "Live Reload",
|
||||
Plugins: []via.Plugin{LiveReloadPlugin},
|
||||
DevMode: true,
|
||||
LogLvl: via.LogLevelDebug,
|
||||
})
|
||||
|
||||
v.Page("/", func(c *via.Context) {
|
||||
@@ -26,7 +27,7 @@ func main() {
|
||||
|
||||
c.View(func() h.H {
|
||||
return h.Div(
|
||||
h.H1(h.Text("Live Reload")),
|
||||
h.H1(h.Text("Live Reload with Via DevMode !!!")),
|
||||
h.P(h.Textf("Count: %d", data.Count)),
|
||||
h.P(h.Span(h.Text("Step: ")), h.Span(step.Text())),
|
||||
h.Label(
|
||||
@@ -38,5 +39,5 @@ func main() {
|
||||
})
|
||||
})
|
||||
|
||||
v.Start(":3000")
|
||||
v.Start()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user