You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
ViteRuby#dev_server_running? always return false when rails and vite run in different containers with skipProxy: true. We can't set the VITE_RUBY_HOST to the name of the vite container, since it need to be something like localhost for the dev server to be reachable from the browser, because we are skipping the proxy. With this configuration ViteRuby#dev_server_running? isn't able to resolve the dev server and assume that it is not running.
Describe the solution you'd like
One solution could be to have a config/env to set the reachable host from the backend code e.g. VITE_RUBY_HOST_FROM_BACKEND and use it here:
Instead of checking network to see if the dev server is running, maybe we could create a file somewhere when starting the dev server and remove it when closing the dev server. To know if the dev server is running we check if the file exist or not, but that solution come with other issues.
Additional context
Here's a monkey patch we are using to bypass the current limitation:
moduleMonkeypatchDevServerRunningmoduleInstanceMethods# original method: https://github.com/ElMassimo/vite_ruby/blob/6249f3b8385c89c6e5f05ad94384cae53d1e5b5d/vite_ruby/lib/vite_ruby.rb#L84-L98defdev_server_running?returnsuperunlessconfig.skip_proxyreturnfalseunlessrun_proxy?return@runningifdefined?(@running) && Time.now - @running_checked_at < 1beginSocket.tcp(ENV["VITE_RUBY_HOST_FROM_BACKEND"] || config.host,config.port,connect_timeout: config.dev_server_connect_timeout).close@running=truerescueStandardError@running=falseensure@running_checked_at=Time.nowendendendclass << selfdefapply_patchconst=find_constmtd=find_method(const)unlessconst && mtd && mtd.arity == 0raise"Could not find class or method when patching ViteRuby#dev_server_running?. Please investigate."endunlessvite_ruby_version_ok?puts"WARNING: It looks like ViteRuby has been upgraded since ViteRuby#dev_server_running? was monkeypatched in #{__FILE__}. Please reevaluate the patch."endconst.prepend(InstanceMethods)endprivatedeffind_constKernel.const_get('ViteRuby')rescueNameErrorenddeffind_method(const)returnunlessconstconst.instance_method(:dev_server_running?)rescueNameErrorenddefvite_ruby_version_ok?version=Gem::Version.create(ViteRuby::VERSION)# The skipProxy config was added in version 3.2.12# see: https://github.com/ElMassimo/vite_ruby/compare/[email protected][email protected]version >= "3.2.12" && version < "4.0.0"endendend# ...MonkeypatchDevServerRunning.apply_patch
The text was updated successfully, but these errors were encountered:
I recently migrated to Vite and faces the same issue, but my solution was different - not to rely on autoBuild at any point, which imho is better to be disabled. Posting my setup for consideration:
I'm always running Vite server manually in a different container, so my autoBuild is set to false in config/vite.json
In dev I'm always running vite server in a separate container, so dev_server_running? can be monkey patched to return Rails.env.development?
For tests in my test_helper.rb I'm building all assets upfront if they are stale (checking ViteRuby.instance.builder.last_build_metadata.stale?)
Not using the proxy middleware in dev, hitting Vite's server directly
That being said, I agree that there should be a way to set something like VITE_RUBY_HOST_FROM_BACKEND to keep autoBuild working correctly.
Yeah, running with container ATM is a PITA. I'm using a docker compose setup with vite running as separate container behing a traefik proxy, and I had to hack a little bit to make it work:
Monkey patched ViteRuby.dev_server_running?
Used the external host value in vite.json port entry
Forced 0.0.0.0 as VITE_RUBY_HOST on the vite container
Another annoying bit, when skipProxy is set to true, vite-ruby overrides the server.origin option, which sets it to 0.0.0.0, overriding the value from the vite.config.js which was properly setup to my dev dns :-/
Is your feature request related to a problem? Please describe.
ViteRuby#dev_server_running?
always returnfalse
when rails and vite run in different containers withskipProxy: true
. We can't set theVITE_RUBY_HOST
to the name of thevite
container, since it need to be something likelocalhost
for the dev server to be reachable from the browser, because we are skipping the proxy. With this configurationViteRuby#dev_server_running?
isn't able to resolve the dev server and assume that it is not running.Describe the solution you'd like
One solution could be to have a config/env to set the reachable host from the backend code e.g.
VITE_RUBY_HOST_FROM_BACKEND
and use it here:vite_ruby/vite_ruby/lib/vite_ruby.rb
Line 91 in 6249f3b
Describe alternatives you've considered
Instead of checking network to see if the dev server is running, maybe we could create a file somewhere when starting the dev server and remove it when closing the dev server. To know if the dev server is running we check if the file exist or not, but that solution come with other issues.
Additional context
Here's a monkey patch we are using to bypass the current limitation:
The text was updated successfully, but these errors were encountered: