Ask A Question

Notifications

You’re not receiving notifications from this thread.

Live Reload with Esbuild in Rails using an EventSource Discussion

got it by googling "live reload esbuild rails", any idea on how to do it?

Reply

The page is not doing live reload, the connection is established but after a rebuild the browser is not refreshing even though the files were bundled again. even if I close the repo I get the this result, it sounds like this content needs a bit more attention, for example if you cave csp rules in the rails initializer the event source connection is blocked. But the biggest problem is that the browser is not refreshing...

Reply

I found the downside with this approach is that if you include certain npm packages it will cause a problem with esbuild that results in some insane memory usage. You'll fly through 10s of gigs of ram just starting your app in local dev. It's a problem that esbuild acknowledges and probably won't go away.

I found this gem that I think is pretty damn awesome: https://github.com/railsjazz/rails_live_reload. It gets me what I want for live reload. For esbuild I still use it but just with a watch flag.

Reply

Where does esbuild acknowledge this issue?

Reply

Hey I need to walk back what I said about esbuild being the issue, the truth is I'm not sure what the issue is exactly. The only thing I can say is that a memory issue does happen with happen at times when starting an app, and if memory serves it's from loading a certain type of npm package. I went deep down the rabbit hole to figure out the issue and never was able to discern what it was exactly. This weekend I'm gonna try to see if I can't reproduce the issue and get a project up to show it. Apologies for my earlier message I'm realizing I don't have enough confidence to proclaim what the issue is. It was really confusing.

Reply

It turns out with Chokidar watching the app/javascript/*/ dir, I have a custom built html 5 web component library git submoduled at app/javascript/rayon. I didn't realize inside of that library was a directory of svg icons containing approximately 23,000 files. This was the cause of the massive RAM usage. I assume Chokidar was setting up to watch all of those files which is crazy. The solution of course is to pay careful attention to what is being watched by Chokidar and to exclude things like a 23,000 file SVG directory. Sorry for the confusion earlier I was thinking of the problem in terms of what was being imported by esbuild, and that was wrong so to be clear:

  1. Live Reload with Esbuild in Rails is perfectly fine.
  2. Esbuild is also perfectly fine.
  3. Chokidar is also perfectly fine.
  4. Starting Esbuild with live reload actually consumes very little RAM normally.
  5. Any massive CPU usage and RAM spiking when starting up the Rails app locally (./bin/dev) might be an indicator that Chokidar is watching a directory with massive amount of files.

Sorry for the confusion.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.