How to add a rails asset dependency to an environment variable with sprockets?


I made the following js.erb:

#= require cable this.App = {}; App.cable = Cable.createConsumer('<%= Rails.application.config.web_socket_server_url %>');

I would like sprockets to regenerate the asset when web_socket_server_url is updated.

I tried to use depend_on, but it only works for files. I also tried to add a config block in an initializer (which I expected reloading all assets when changed, instead of just the one concerned):

Sprockets.register_dependency_resolver 'web-socket-server-url' do ::Rails.application.config.web_socket_server_url end config.assets.configure do |env| env.depend_on 'web-socket-server-url' end

I got the idea after seeing this commit of sprocket-rails <a href="https://github.com/rails/sprockets-rails/commit/9a61447e1c34ed6d35c358935bcae4522b60b48d" rel="nofollow">https://github.com/rails/sprockets-rails/commit/9a61447e1c34ed6d35c358935bcae4522b60b48d</a>

But this did not work as I would have expected.

Ideally, I would have hoped to be able to register the dependency resolver in my initializer, and then adding //= depend_on 'web-socket-server-url' in my asset, so only the asset would be reloaded.

As a workaround, I might add the config in the HTML markup, and get in in the javascript without using ERB, but it does not feel as good.

How could I make this work with sprockets ?


The current API for that is the one that you already used.

Sprockets.register_dependency_resolver 'web-socket-server-url' do ::Rails.application.config.web_socket_server_url.to_s end config.assets.configure do |env| env.depend_on 'web-socket-server-url' end

That would invalidate all the cache when the config is changed an not the cache for that file as you pointed.


  • How to reload a page only once everytime it gets loaded
  • Paperclip in Production with Unicorn: uninitialized constant Paperclip (NameError)
  • XMLHttpRequest in IE-7
  • Using Recaptcha with EPiServer XForms
  • How can integers (keys & values) be added and retrieved from a NSDictionary
  • Chrome breakpoint on radio doesn't fire
  • Responsive left sidebar open close
  • Swift Initialization Rule Confusion
  • Prevent page break in text block with iText, XMLWorker
  • C++ friend class std::vector
  • Dart - Isolate Cross Window Communication
  • Rest Services conventions
  • SyntaxError: (irb):26: both block arg and actual block given
  • How can I replace the server in Web Component Tester
  • How to set elevation color?
  • Best way to dynamically load an xml configuration file into a Flex4/Flash movie at runtime?
  • Selenium to click on a javascript button corresponding to a text
  • How can I speed up CURL tasks?
  • htaccess add www if not subdomain, if subdomain remove www
  • HTTP/2 streams vs HTTP/1.1 connections
  • PHP buffered output depending on server setting?
  • MVC3 Razor - ListBox pre-select not working
  • Functions in global context
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • Is possible to count alias result on mysql
  • Check if a string to interpolate provides expected placeholders
  • ILMerge & Keep Assembly Name
  • Large data - storage and query
  • How to get next/previous record number?
  • WOWZA + RTMP + HTML5 Playback?
  • Confusion with PayPal's monthly billing cycle
  • How do I rollback to a specific git commit
  • Is there a mandatory requirement to switch app.yaml?
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • Busy indicator not showing up in wpf window [duplicate]
  • UserPrincipal.Current returns apppool on IIS