![](/img/trans.png)
[英]Wrong fingerprint in Rails 3.1 asset pipeline for JS files in production
[英]Get the asset url without fingerprint in Rails
使用javascript_url
我們可以獲得資產的網址:
<script src="<%= javascript_url 'company_widget' %>"
token="<%= current_user.token %>"
class="ofri-company-widget"
></script>
但是, javascript_url
返回帶指紋的網址:
domain.com/assets/company_widget-<fingerprint>.js
它似乎永遠緩存,每當我們需要在腳本中進行更改時,使用此腳本的第三方將不得不重新加載它。
我注意到訪問沒有指紋的文件也有效:
domain.com/assets/company_widget.js
有沒有辦法告訴javascript_url
不添加指紋? 或者在這種情況下還有另一種更好的解決方案
http://guides.rubyonrails.org/asset_pipeline.html#turning-digests-off
您可以通過更新config / environments / development.rb來關閉摘要以包括:
config.assets.digest = false
您真正需要做的是在控制器操作中返回您的javascript文件,因此可以控制此特定文件的緩存。
在您的控制器操作中,您應該執行以下操作:
def company_widget
response.headers["Expires"] = 1.day.from_now.httpdate
expires_in 1.day, public: true, must_revalidate: true
format.js do
asset = open(ActionController::Base.helpers.asset_url('company_widget.js', host: host))
send_data asset.read, :type => asset.content_type
end
end
def host
request.protocol + request.host_with_port
end
然后,您可以指向控制器操作,並確保您可以控制返回的內容。
<script src="http://example.com/company_widget.js"></script>
Expires
頭是設置緩存的舊方法,而expires_in
Rails方法將設置Cache-Control
頭,這是管理緩存的最佳方式。 它優先於Expires
。
另外,您可以查看有關緩存的精彩文章: https : //www.mnot.net/cache_docs/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.