htmgo/h/livereload.go

34 lines
561 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() Renderable {
2024-09-12 02:06:34 +00:00
return Div(Get("/livereload"), Trigger("every 2s"))
2024-09-11 00:52:18 +00:00
}
func AddLiveReloadHandler(path string, app *fiber.App) {
app.Get(path, LiveReloadHandler)
}