a-h / templ

A language for writing HTML user interfaces in Go.
https://templ.guide/
MIT License
8.33k stars 274 forks source link

bug: `templ generate --watch --proxy` triggers 2 browser reload events #891

Open atipugin opened 3 months ago

atipugin commented 3 months ago

Before you begin

Please make sure you're using the latest version of the templ CLI (go install github.com/a-h/templ/cmd/templ@latest), and have upgraded your project to use the latest version of the templ runtime (go get -u github.com/a-h/templ@latest)

Done.

Describe the bug For some reason templ generate --watch processes same file multiple times on a single save. As result, it triggers several page reloads per change. I disabled all formatters to ensure there's nothing to emit another fs event, but it didn't help.

I've tried to reproduce it inside devcontainer (used mcr.microsoft.com/devcontainers/go:1-1.22-bookworm) and everything works just fine. So I guess the issue is with macOS FS events stuff.

To Reproduce

  1. Start templ generate --watch -v --proxy="http://localhost:8080" --cmd="go run cmd/myapp/main.go"
  2. Change .templ file

Expected behavior It should emit only one reload event.

Screenshots

SCR-20240819-opao

https://github.com/user-attachments/assets/db8c8ad0-76f0-4daf-a44b-eb9a6e94c465

Logs None.

templ info output

usage: templ <command> [<args>...]

templ - build HTML UIs with Go

See docs at https://templ.guide

commands:
  generate   Generates Go code from templ files
  fmt        Formats templ files
  lsp        Starts a language server for templ files
  version    Prints the version

Desktop (please complete the following information):

Additional context None.

joerdav commented 3 months ago

I think the issue here is that templ updates the .txt file, but also is has a file watcher on that dir. And then triggers a reload as textUpdated is set to true: https://github.com/a-h/templ/blob/main/cmd/templ/generatecmd/eventhandler.go#L115

The only reason I can see that we would do this is if we were expecting that processes outside of templ to be able to update the dev mode files. @a-h My suggestion is that we do not return updated in this case so that text changes don't result in a double reload?