Ruby on Rails
Wednesday, April 6, 2016
When working in development mode (using webrick) it is painful waiting for pages to load when there are a lot of css and js assets that are loaded one by one.
This has annoyed me for years but today it dawned on me how easy the fix is. I'm using Rails 4.1.x
in development.rb
Add the digest to assets
config.assets.digest = true
and then add middleware that caches assets for a year (in development.rb only, in prod we cache via apache/nginx).
config.middleware.use CacheAssets
My CacheAssets class looks like so
class CacheAssets
def initialize app
@app = app
end
def call env
res = @app.call(env)
path = env["REQUEST_PATH"]
if path =~ /assets/
if res[0] == 200
res[1]["Expires"] = (Time.now + 1.year).utc.rfc2822
res[1]["Cache-Control"] = 'public'
end
return res
end
res
end
end
If I edit any CSS or JS file it will get a new digest key so there is no need to clear the cache to pick up my latest changes while developing.
In the interest of making the development environment faster by default, maybe this should be baked into Rails 5? disclaimer, i haven't used Rails 5 yet so maybe something like this is already there.
Thoughts?
Tony
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bb45f734-5f0e-4a0d-b2d7-feb683916db3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment