-
-
Notifications
You must be signed in to change notification settings - Fork 277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd: rebuild and refresh when a go file is changed in --watch
mode
#646
Comments
Templ only watches templ files, I recommend using templ watch in conjunction with air, similar to my example here: |
Yes, just the Go files generated by templ, as per: templ/cmd/templ/generatecmd/watcher/watch.go Lines 57 to 68 in ad69501
However, it would be no trouble to update it to monitor all |
We could, my worry is that theres entire projects based on watching Go projects. I can imagine the templ watch tool gaining more complexity as we add more features that air covers. Though, we could have a crude watch feature that covers 80% of cases, then recommend other tools if theres more niche scenarios. Maybe I'm a bit biased since I'm very much enjoying the air+templ DX! |
I think doing the 80%, and making sure we support the use of I added the change to not do all files here: dd1ee1e but maybe I was being over-cautious with that... I think I could change it to include |
Makes sense, we could look at including a glob or regex param to help cover the 80%, unless you think that would be too much too soon? |
I stumbled across this with lots of search. I was using air to reload, but I miss the browser auto refresh feature. Initially I used browser-sync's proxy mode. However, I just couldn't reliably get the reload event to sent: It has to be after the server is restarted. In a way, I much prefer a polling method.
I don't mind the polling interval to be shorter as well, I'm fine trading more cpu with refresh speed. Thanks & Regards |
Aha, actually found a pretty good enough solution:
So |
Can I make one small suggestion: Add This makes other watcher to send the browser refresh event down easily. Use case: I have tailwindcss watching files and generating css. Although this is mostly covered by changes in
If this feature is added, I could easily do a |
@a-h are you willing to accept a PR that adds |
Thanks for writing that up into a nicely written up proposal @jackielii. This issue can remain focused on whether templ should also support hot reload for I'm currently thinking that it should include |
I think there might be a problem with it: Imagine if we include templ file change
go file changes
So the question is should templ send the browser reload event on 1 or 3? Intuitively it should send reload event on step 3, but: Even if templ didn't send the reload event, the browser will still send a request because the SSE connection will try to re-connect ref The above are my suspicions, I didn't dig into them. However, the former does happen. You can test it in my setup #667, I talked about my suspicion here: #596 (reply in thread) Finally, there is another case already handled, but worth mentioning: When the go code related part of a templ file changes, the Update: After a few tests, I think sending reload event in 1 or 3 in the go file changes will both work: When the reload event is sent to the browser, the proxy server should be connected, so there is no "reconnect" happening here. The reload will hang for a bit to wait for the server to become available. |
I agree that it would be fine to trigger based on go files changing in the case of Do we still want to add this feature? |
--watch
mode
I think we are in agreement that we should be able to watch more than just templ files. I've also received feedback that some would like to watch on more than just go and templ files. Use cases include embeded css files that require a rebuild if changed. My suggestion is to switch to one flag:
The default of it would be Air users could override to And users who want to build on a css change could do We could avoid using regex if we wanted, opt for some kind of comma separated glob instead. |
I also was going through https://templ.guide/commands-and-tools/live-reload and thought it would watch all my .go files and was surprised it didn't work as (I) expected. So I was about to open an issue, but first searched the existing issues using I think its a good decision to focus on the templ generated files and so I fully agree with that, but my suggestion would be to clearly mention this on the above doc page and also state that it is a conscious decision (maybe even mention this issue). edit: I created a PR with a possible suggestion for the note @ #896 Feel free to edit it before merging of course. |
+1 for making templ to watch go files. |
+1 would also like to have this feature, then i could only use |
I have tried the example given in the docs: https://templ.guide/commands-and-tools/hot-reload
When trying to comment-out for example the "/" route, and then saving the
main.go
file, I don't see any reloads in my terminal and the route also still exists (when I refresh in the browser it still sends me the page).I didn't find any related issues here and it's unclear to me how to debug this since no error or anything is logged.
The only thing that maybe causes this (given that it works for others (?)) is, that I am developing on NixOS.
I am using the following
flake.nix
:I use it via
nix develop
and run the given example command:templ generate --watch --proxy="http://localhost:8080" --cmd="go run ."
The text was updated successfully, but these errors were encountered: