Hot-reload with templ proxy and air #596
leandergangso
started this conversation in
Show and tell
Replies: 1 comment 10 replies
-
|
The problem with this is there is always the race condition where your server is not ready, but browser reload sse event is already sent down. The sequence of events goes:
#661 tries to address this problem. With it, you should be able to write Makefile: live/templ:
templ generate --watch --proxy="http://localhost:8080" --open-browser=false -v
live/server:
go run github.com/cosmtrek/air@v1.51.0 \
--build.cmd "go build -o tmp/bin/main" --build.bin "tmp/bin/main" --build.delay "100" \
--build.exclude_dir "node_modules" \
--build.include_ext "go" \
--misc.clean_on_exit true
live/tailwind:
npx tailwindcss -i ./input.css -o ./assets/styles.css --minify --watch
live/esbuild:
npx esbuild js/index.ts --bundle --outdir=assets/ --watch
live/sync_assets:
go run github.com/cosmtrek/air@v1.51.0 \
--build.cmd "templ generate --notify-proxy" \
--build.bin "true" \
--build.delay "100" \
--build.exclude_dir "" \
--build.include_dir "assets" \
--build.include_ext "js,css"
live:
make -j5 live/templ live/server live/tailwind live/esbuild live/sync_assetsThis approach won't re-build your Go binary unless Go related file changes. But it will reload the browser if any templ file, bundled js, or css file changes. Obviously if you're using //go:embed directive to embed assets, then you do need to rebuild the Go binary. |
Beta Was this translation helpful? Give feedback.
10 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I wanted to use
air(for obvious reasons) as well as thetempl proxyto have hot-reloading in my browser, and this is the solution I've come up with.It works by running
airandtemplseparately, and using the templ -cmd flag to run a script that will trigger a new rebuild in air.Do note that sometimes
airis not fast enough to rebuild GO beforetemplhas sent the reload event and the browser reloaded, in those cases you'll have to do a manual reload.Also it seems like there are some optimization that can be done in templ to better determine when rebuilding GO is needed.
Anyhow, here are the files, just copy them into your project and run
make devto start your app. 😄airtempl(make it executable:chmod +x airtempl)makefileair.tomlBeta Was this translation helpful? Give feedback.
All reactions