如何更改 Rails asset_tag URL 的格式?

发布于 2024-10-08 12:15:13 字数 982 浏览 0 评论 0原文

背景

我目前正在开发一个 Rails 3 项目,打算将其与 Amazon CloudFront 一起使用,并使用指向我实际 Web 服务器的自定义源。这意味着我的 Web 服务器将告诉浏览器从 CloudFront 检索静态资产,如果 CloudFront 缓存了该资产,它将返回它,如果没有,它将使用与我的 Web 服务器相同的路径检索它,然后缓存它以供将来使用请求。

问题

Amazon CloudFront 在确定是否应该破坏自己的缓存以从其来源重新下载资产时,会删除并忽略查询字符串参数。这显然给 Rails 使用 RAILS_ASSET_ID 生成 URL 的方式带来了问题,即:/assets/path/to/asset.ext?RAILS_ASSET_ID。使用默认资产 URL,CloudFront 将保留过时资产长达 24 小时。

解决方案?

我希望做的是以某种方式覆盖 Rails 3 生成资产标签的方式,以便我可以生成类似 /assets/RAILS_ASSET_ID/path/to/asset.ext.我已经有了一个如何让 Web 服务器响应这些 URL 的解决方案,我只需要弄清楚如何让 Rails 生成 URL。

注释

  1. 我的 RAILS_ASSET_ID 将是我最新的 git 提交哈希的子字符串。该设置将由 capistrano 在部署过程中处理。 我可以在每次部署中清除所有资产缓存,我不需要特定于资产的 ID
  2. 我已经尝试覆盖 ActionView::Helpers::AssetTagHelper.rewrite_asset_path但它似乎不起作用,从我读到的内容来看,这是一种不应该依赖的私有方法。
  3. 先感谢您!

Background

I'm currently working on a Rails 3 project that I intend to use with Amazon CloudFront with a custom origin pointing back to my actual web server. This means that my web server will tell the browser to retrieve static assets from CloudFront, if CloudFront has the asset cached it will return it, if it does not it will retrieve it using the same path from my web server and then cache it for future requests.

Problem

Amazon CloudFront drops and ignores query string parameters when determining whether it should bust it's own cache to re-download the asset from it's origin. This obviously poses a problem with the way Rails generates URLs with the RAILS_ASSET_ID which is to do: /assets/path/to/asset.ext?RAILS_ASSET_ID. With the default asset URLs CloudFront will hold onto stale assets for up to 24 hours.

Solution?

What I'm hoping to do is somehow override the way Rails 3 generates asset tags so that I can generate URLs like /assets/RAILS_ASSET_ID/path/to/asset.ext. I already have a solution for how to make the web server respond to those URLs, I just need to figure out how to make Rails generate the URL.

Notes

  1. My RAILS_ASSET_ID will be a sub-string of my latest git commit hash. Setting this will be handled by capistrano during the deployment process. I am OK with busting all asset caches on each deployment, I don't need asset-specific IDs
  2. I have already attempted to override ActionView::Helpers::AssetTagHelper.rewrite_asset_path but it didn't seem to work and from what I read this is a private method that should not be relied upon.
  3. Thank you in advance!

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

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

发布评论

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

评论(2

吲‖鸣 2024-10-15 12:15:13

“将 ActionController::Base.config.asset_path 设置为一个过程,该过程采用未修改的资源路径并返回资源缓存工作所需的路径。”

这是来自 AssetTagHelper 文档。

"Set ActionController::Base.config.asset_path to a proc that takes the unmodified asset path and returns the path needed for your asset caching to work."

That's from the AssetTagHelper documentation.

地狱即天堂 2024-10-15 12:15:13

asset_id 插件 应该可以解决您的问题。它支持 Amazon S3 和 CloudFront。

The asset_id plugin should solve your problem. It supports Amazon S3 and CloudFront.

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