htmgo/h/livereload.go

34 lines
559 B
Go
Raw Normal View History

2024-09-11 00:52:18 +00:00
package h
import (
"github.com/gofiber/fiber/v2"
"strconv"
"time"
)
var Version = time.Now().Nanosecond()
func LiveReloadHandler(c *fiber.Ctx) error {
v := strconv.FormatInt(int64(Version), 10)
current := c.Cookies("version", v)
if current != v {
c.Set("HX-Refresh", "true")
}
c.Cookie(&fiber.Cookie{
Name: "version",
Value: v,
})
return c.SendString("")
}
func LiveReload() *Node {
2024-09-11 21:08:35 +00:00
return Div(Get("/livereload"), Trigger("every 200ms"))
2024-09-11 00:52:18 +00:00
}
func AddLiveReloadHandler(path string, app *fiber.App) {
app.Get(path, LiveReloadHandler)
}