var srvHandler http.Handler // Handler to serve your HTML
var log *slog.Logger // Your slog logger
srvHandler, err = reload.NewMiddleware(
// Pass in the old HTML handler.
srvHandler,
// Pass in the directory you want to watch.
// When a file in this directory changes, the browser will reload.
serveDir,
// Optionally, pass in a logger to be notified of detected file changes.
log,
)
if err != nil {
return fmt.Errorf("failed to create reload middleware: %w", err)
}
Script is the JavaScript snippet that enables auto-reloading in the browser.
Embed this into your index.html or equivalent.
Heavily inspired by https://github.com/aarol/reload, licensed under MIT.