将不经常更改的 JSON 数据发送到生产中的客户端的最佳方法

发布于 2024-12-21 23:28:34 字数 572 浏览 2 评论 0原文

我正在使用 Rails 3.1 开发一个极其依赖 JavaScript 的应用程序。我开发了一个“管理”区域,可以使用 Rails 模型通过 Web 界面自行配置应用程序。数据库的这一部分预计很少更改,但我需要有能力更改它,因此管理员。

我现在正在研究将数据传输到浏览器的最佳方法。我需要客户端在 JavaScript 应用程序本身启动之前加载数据,因此我希望尽可能避免任何 AJAX 调用,因为必须首先完成这些后续请求。

由于数据很少更改,因此它是缓存的绝佳候选者 - 但在这种情况下我无法使用资产管道,因为我不想在模型数据更改时重新部署 Rails 应用程序。

我也无法在资产中使用嵌入的 ERB 代码,因为即使我不预编译它们,实际的资产文件也必须在 Rails 接受修改之前发生更改。即使模型数据已更改,由于 ERB 代码未修改,Rails 也不会为我生成新的资产文件,即使我触摸该文件(以更新时间戳),因为它的 MD5 哈希值未更改。

此时,我将分解 JSON 模型并将其直接嵌入到视图模板中,但我不愿意这样做(数据和视图的分离,等等)。

是否有更好的解决方案,例如强制 Rails/Sprockets 重新编译特定资产或全部资产的编程方式?

I'm developing an extremely JavaScript-heavy application using Rails 3.1. I have developed an "admin" area where the app can be configured by myself through a Web interface, using Rails models. This portion of the database is expected to change very infrequently, but I need to have the ability to change it, hence the admin.

I'm now working out the best way to get the data down to the browser. I need the client to load the data before the JavaScript application itself can be started, so I'd like to avoid any AJAX calls if possible, because these subsequent requests would have to be completed first.

Since the data rarely changes, it's a great candidate for caching -- but I can't make use of the asset pipeline in this case because I don't want to have to re-deploy the Rails app whenever the model data changes.

I can't use embedded ERB code in the assets either because, even if I don't precompile them, the actual asset file has to have changed before Rails will pick up the modifications. Even though the model data has changed, since the ERB code is not modified, Rails won't produce a new asset file for me, even if I touch the file (to update the timestamp), because its MD5 hash hasn't changed.

At this point I'm about to break down and embed the JSON models directly into the view templates, but I'm loathe to do this (separation of data and view, and all that).

Are there any better solutions, such as a programmatic way to force Rails/Sprockets to recompile a particular asset, or all of them?

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

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

发布评论

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

评论(1

虫児飞 2024-12-28 23:28:34

目前,我决定使用 respond_to 为其创建一个控制器和 JavaScript 视图。示例:

class ConfigurationController < ApplicationController
  def index
    respond_to do |fmt|
      fmt.js
      # ...
    end
  end
end

此控制器的 JS 视图吐出模型数据:

<% InfrequentlyChangingModel.all.each do |record| %>
  AppData[<%= record.id %>] = (<%= record.to_json.html_safe %>);
<% end %>

然后在前端视图中,我像任何其他 JavaScript 文件一样引用它:

<%= javascript_include_tag 'configuration' %>

我仍然愿意接受其他建议,但这是最干净、最像 Rails 的解决方案到目前为止我已经能够想出。它会预先产生一个额外的请求(针对 js 文件),但应该很好地缓存并避免额外的 AJAX。

For now, I've decided to create a controller and JavaScript view for it, using respond_to. Example:

class ConfigurationController < ApplicationController
  def index
    respond_to do |fmt|
      fmt.js
      # ...
    end
  end
end

The JS view for this controller spits out the model data:

<% InfrequentlyChangingModel.all.each do |record| %>
  AppData[<%= record.id %>] = (<%= record.to_json.html_safe %>);
<% end %>

Then in the front-end view I reference it like any other JavaScript file:

<%= javascript_include_tag 'configuration' %>

I'm still open to other suggestions, but this is the cleanest and most Rails-like solution I've been able to come up with so far. It produces an extra request up-front (for the js file) but should cache pretty nicely and avoids extra AJAX.

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