Reputation: 6246
I have a .js.erb file in my assets directory. This reads some locale specific config.
However when the underlying config changes the new file is not being served to my browser. I get 304 not modified.
If I change the .js.erb file by adding white space the new file is correctly served. Doing that each time I add config would be a pain.
Is there a way to configure rails just to not cache this particular file?
Thanks for any suggestions.
Edit: Done a bit more reading on the asset pipeline
"Assets are compiled and cached on the first request after the server is started. Sprockets sets a must-revalidate Cache-Control HTTP header to reduce request overhead on subsequent requests — on these the browser gets a 304 (Not Modified) response.
If any of the files in the manifest have changed between requests, the server responds with a new compiled file."
So the problem here is the first request is cached - this is a dynamic javascript file. Is perhaps the only way to prevent this being cached to inline the javascript??
Upvotes: 3
Views: 2129
Reputation: 84114
It sounds like you don't want to use the asset pipeline, which is trying very hard to promote caching.
Rather than trying to fight the asset pipeline, create an actual controller and then you'll be able to set the cache control headers to your liking.
If you have a js.erb template in app/views/controller_name then rails should just render it.
For example if you had a controller called JsController you could add
match '/javascripts/settings.js', :controller => :js, :action => :settings, :format => :js, :as => :setting_js
to your routes.rb file and then stick settings.js.erb in app/views/js
You can then link to it with
= javascript_include_tag settings_js_path
Upvotes: 3
Reputation: 2153
Have you tried something like this in the controller method that serves that file?
response.headers["Last-Modified"] = Time.now.httpdate
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
response.headers["Cache-Control"] = 'no-store, no-cache, must-revalidate, max-age=0, pre-check=0, post-check=0'
Upvotes: 2