Rails资产管道:如何防止缓存特定资产

编程入门 行业动态 更新时间:2024-10-27 23:28:26
本文介绍了Rails资产管道:如何防止缓存特定资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如标题中所述,我要防止缓存特定资产,即javascript文件something.js.erb.情况如下:

As stated in the title I want to prevent caching of a specific asset, namely a javascript file something.js.erb. The situation is like as follows:

something.js.erb的内容:

... var something = <%= SomethingHelper.get_something.to_json %>; ...

它成功绑定了SomethingHelper中的值,但仅绑定了一次,除非手动编辑了javascript文件,否则就不会再分配var something的值.

It binds the value from SomethingHelper successfully but only once and unless the javascript file is edited by hand the value of var something is never assigned again.

这可能会以某种方式出现,但显然不能满足我的需求. SomethingHelper.get_something的输出根据通话时间而变化.因此,我需要在编译的something.js文件中查看最新数据.

This might be somehow expected but clearly doesn't meet my needs. Output of SomethingHelper.get_something changes according to call time. So I need to see up-to-date data in my compiled something.js file.

我的确切需求:

  • 我不想整体禁用资产管道缓存
  • 我只希望每次请求something.js.erb时都将其渲染.
  • I don't want to disable asset pipeline caching as a whole
  • I only want something.js.erb to be rendered every time it is requested.

这甚至有可能吗?

环境信息:

  • 路轨4
  • 开发模式
  • Rails自己的服务器,但将在prod上的nginx上

谢谢

推荐答案

您正在将前端业务逻辑与数据结合起来.这是不可取的,这也是我在大多数情况下不使用或不建议使用ERB + JS的原因之一(尤其是像Rails教程和指南一样急于触发响应行为).你也更好...

You're marrying front-end business logic with data. This is inadvisable, and one of the reasons I don't use or recommend using ERB + JS for most scenarios (especially triggering behavior on response like Rails tutorials and guides are keen on doing). You are better off either…

  • 取消请求以从JavaScript中获取数据.
  • 假设该变量将在每个页面(或接近页面)上使用,并且是相对简短的非二进制数据,则您可以在布局中嵌入meta标记以及相关信息.
  • Firing a request off to fetch the data from your JavaScript.
  • Provided the variable is going to be used on every page (or close to it) and is relatively brief, non-binary data, you can embed a meta tag in your layout with the relevant information.
  • 例如:

    # /app/views/layouts/application.html.erb <%= tag :meta, name: 'something', content: @something %> # /app/assets/javascripts/application.js $('meta[name="something"]').attr('content');

    更多推荐

    Rails资产管道:如何防止缓存特定资产

    本文发布于:2023-07-08 01:07:44,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1070202.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:资产   缓存   管道   如何防止   Rails

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!