Forum: Ruby on Rails Should rails force asset caching in development?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Tony P. (Guest)
on 2016-04-06 22:52
(Received via mailing list)
When working in development mode (using webrick) it is painful waiting
pages to load when there are a lot of css and js assets that are loaded
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

  def call env
    res =
    path = env["REQUEST_PATH"]
    if path =~ /assets/
      if res[0] == 200
        res[1]["Expires"] = ( + 1.year).utc.rfc2822
        res[1]["Cache-Control"] = 'public'
      return res

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
5 yet so maybe something like this is already there.

This topic is locked and can not be replied to.