I’m implementing a small hobby project with Go, HTMX, Templ as TechStack. Because I’m new to it, I started a small playground and try to build some small search.
Now I’m facing the issue, that my handler method is only called once at beginning:
package main
import (
"net/http"
"github.com/go-chi/chi/v5"
)
func main() {
router := chi.NewRouter()
router.Get("/", indexHandler().ServeHTTP)
router.Get("/employee_search", employeeSearchHandler().ServeHTTP)
http.ListenAndServe(":3000", router)
}
The basic idea is, that if someone is search in a search-box the employeeSearchHandler is updating the UI to show some results.
At the moment the handler is only generating a random number:
func employeeSearchHandler() *templ.ComponentHandler {
var employee = rand.IntN(1000)
fmt.Println(employee)
return templ.Handler(employee_search.EmployeeResult(employee))
}
My Template looks like this:
templ EmployeeSearch() {
<input type="search" name="search" placeholder="Suchen" aria-label="Search" autocomplete="off"
hx-get="/employee_search"
hx-trigger="input delay:500ms changed"
hx-target="#employee_result"
hx-swap="innerHTML" />
<div id="employee_result">
</div>
}
Now I’m wondering that my browser shows a new request every time I’m typing and stopping for 500ms as expected, but it returns always the exact same HTML. The random number isn’t changing. I see this behaviour also in Server Console. The number is generated on startup and afterwards it’s never created again. Even the HTMX event is firing, but it just send back the same HTML all the time.
What do I oversee here?