当 config.assets.initialize_on_precompile 为 false 时设置常量

发布于 2024-12-28 13:35:27 字数 370 浏览 3 评论 0原文

在 Heroku 上部署需要 application.rb 中的 config.assets.initialize_on_precompile=false。从 Rails Guide 中,它说这“部分加载您的应用程序”。

所以我想知道设置在编译资产中使用的一些常量(例如 *.js.erb)的最佳实践是什么?将此标志设置为 false 后,我无法使用初始化程序中设置的任何内容。我不太清楚加载了应用程序的哪些部分,以及在执行编译步骤之前如何设置任何常量、变量等。

谢谢!

Deploying on Heroku requires that config.assets.initialize_on_precompile=false in application.rb. From the Rails Guide it says that this "partially loads your application".

So I was wondering what the best practices were for setting some constants to be used in compiled assets (e.g. *.js.erb)? With this flag set to false I can't use anything that is set in initailizers. I'm not super clear on what parts of the application are loaded, and how I might be able to set any constants, variables etc before the compilation step is performed.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谈情不如逗狗 2025-01-04 13:35:27

我遇到了类似的情况,幸运的是我能够轻松解决它。我知道 config.assets.initi.... 正在 Heroku 上按照 Heroku 文档的要求执行。在此之前我只需要手动加载初始化程序文件。我只想要一个 /config/initializers/settings.rb 文件,因此我添加了一个要求。这解决了问题。

...
#at the end of the /config/application.rb file
require Rails.root.join("config/initializers/settings") #this is not loaded automatically in the assets:precompile task, that's why we need this
config.assets.initialize_on_precompile = false
...

I ran into a similar situation, fortunately I was able fix it easily. I knew that the config.assets.initi.... was being executed on heroku as heroku docs ask for it. I just had to manually load my initializer files before that. I wanted just one /config/initializers/settings.rb file, so I added a require for that. And that fixed the problem.

...
#at the end of the /config/application.rb file
require Rails.root.join("config/initializers/settings") #this is not loaded automatically in the assets:precompile task, that's why we need this
config.assets.initialize_on_precompile = false
...
猛虎独行 2025-01-04 13:35:27

这是我能想到的最干净的解决方案

somejavascriptfile.js.erb

<%
  # get around no asset initialization in precompile
  require Rails.root.join('config/initializers/facebook')
%>

console.log('<%= Facebook::APP_ID %>');

config/initializers/facebook.rb

module Facebook
  CONFIG = YAML.load(File.open(Rails.root.join('config/facebook.yml')))[Rails.env]
  APP_ID = CONFIG['app_id']
  SECRET = CONFIG['secret']
end

Here's the cleanest solution I could come up with

somejavascriptfile.js.erb:

<%
  # get around no asset initialization in precompile
  require Rails.root.join('config/initializers/facebook')
%>

console.log('<%= Facebook::APP_ID %>');

config/initializers/facebook.rb

module Facebook
  CONFIG = YAML.load(File.open(Rails.root.join('config/facebook.yml')))[Rails.env]
  APP_ID = CONFIG['app_id']
  SECRET = CONFIG['secret']
end
同尘 2025-01-04 13:35:27

我对资产管道有点陌生,所以我不确定最佳实践。然而,我想到的一个解决方案是添加一个 rake 任务,该任务是 asset:precompile 的依赖项。我不太确定这些任务会做什么。我想象的实现不是很漂亮。

您可能必须创建模板文件,例如 *.js.erb.template,您的 rake 任务将其复制到 *.js.erb 文件,并对文件内容执行某种 gsub 以用常量值替换某些占位符。

希望其他人有更好的方法...

I'm somewhat new to the asset pipeline, so I'm not sure about best practices. However, one solution that comes to mind is to add a rake task that is a dependency of assets:precompile. I'm not quite sure what the taks would do though. The implementation I'm imagining isn't very pretty.

You'd probably have to create template files, like *.js.erb.template that your rake task would copy to *.js.erb files and perform some kind of gsub on the file contents to replace some placeholders with constant values.

Hopefully someone else has a better method...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文