问题描述
我制作了以下js.erb:
I made the following js.erb:
#= require cable
this.App = {};
App.cable = Cable.createConsumer('<%= Rails.application.config.web_socket_server_url %>');
我希望链轮在web_socket_server_url
更新时重新生成资产.
I would like sprockets to regenerate the asset when web_socket_server_url
is updated.
我尝试使用depend_on,但是它仅适用于文件.我还尝试在初始化程序中添加一个配置块(我希望在更改时重新加载所有资产,而不仅仅是相关的资产):
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
在看到sprocket-rails的提交后,我有了主意. com/rails/sprockets-rails/commit/9a61447e1c34ed6d35c358935bcae4522b60b48d
I got the idea after seeing this commit of sprocket-rails https://github.com/rails/sprockets-rails/commit/9a61447e1c34ed6d35c358935bcae4522b60b48d
但是,这并没有达到我的预期.
But this did not work as I would have expected.
理想情况下,我希望能够在初始化程序中注册依赖项解析器,然后在资产中添加//= depend_on 'web-socket-server-url'
,以便仅重新加载资产.
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.
作为一种解决方法,我可以在HTML标记中添加配置,并在不使用ERB的情况下进入javascript,但这并不理想.
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.
我该如何使用链轮呢?
推荐答案
该API的当前API是您已经使用的API.
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.
这篇关于如何使用链轮将Rails资产依赖项添加到环境变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!