Get the URL of an asset without a fingerprint in Rails

Using javascript_url, we can get the asset url:

&lt;script src="<%= javascript_url 'company_widget' %>"
  token="<%= current_user.token %>"
  class="ofri-company-widget"
&gt;&lt;/script&gt;

However, it javascript_urlreturns the URL with the fingerprint:

domain.com/assets/company_widget-<fingerprint>.js

This seems to be cached forever, and whenever we need to make changes to a script, third-party users using this script will have to reload it.

I noticed that accessing the file without fingerprints also works:

domain.com/assets/company_widget.js

Is there any way to say javascript_urlnot to add a fingerprint? Or is there another better solution in this context?

+4
source share
2 answers

What you really need to do is put your javascript file back in the controller action in order to have cache control for that particular file.

- :

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 header - , expires_in Rails Cache-Control, . Expires.

, : https://www.mnot.net/cache_docs/

+1

http://guides.rubyonrails.org/asset_pipeline.html#turning-digests-off

, config/environment/development.rb, :

config.assets.digest = false

+2

Source: https://habr.com/ru/post/1693064/


All Articles