Skip to content

Commit

Permalink
Configure EmberCli::Deploy::File cache headers
Browse files Browse the repository at this point in the history
Closes [#401].

Previously, requests for Ember assets were being proxied through
`EmberCli::Deploy::File` Rack middleware without the HTTP
`Cache-Control` header.

Ember `production` builds digest the filenames of images/css/js/etc.
This makes these files highly cachable.

This commit configures the underlying `Rack::File` middleware to serve
assets with the `Cache-Control` header set by Rails.

[#401]: #401
  • Loading branch information
seanpdoyle committed Feb 5, 2016
1 parent 7685862 commit 4c67d23
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
master
------

* `EmberCli::Deploy::File` serves assets with Rails' `static_cache_control`
value. [#403]

[#403]: https://github.com/thoughtbot/ember-cli-rails/pull/403

0.7.1
-----

Expand Down
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,22 @@ As long as your [CDN is configured to pull from your Rails application][dns-cdn]

### Deployment Strategies

By default, EmberCLI-Rails will serve the `index.html` file that `ember build`
produces.
By default, EmberCLI-Rails uses a file-based deployment strategy that depends on
the output of `ember build`.

Using this deployment strategy, Rails will serve the `index.html` file and other
assets that `ember build` produces.

These EmberCLI-generated assets are served with the same `Cache-Control` headers
as Rails' other static files:

```rb
# config/environments/production.rb
Rails.application.configure do
# serve static files with cache headers set to expire in 1 year
config.static_cache_control = "public, max-age=31622400"
end
```

If you need to override this behavior (for instance, if you're using
[`ember-cli-deploy`'s "Lightning Fast Deployment"][lightning] strategy in
Expand Down
8 changes: 7 additions & 1 deletion lib/ember_cli/deploy/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def mountable?
end

def to_rack
Rack::File.new(app.dist_path.to_s)
Rack::File.new(app.dist_path.to_s, rack_headers)
end

def index_html
Expand All @@ -28,6 +28,12 @@ def index_html

attr_reader :app

def rack_headers
{
"Cache-Control" => Rails.configuration.static_cache_control,
}
end

def check_for_error_and_raise!
app.check_for_errors!

Expand Down
4 changes: 4 additions & 0 deletions spec/dummy/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

module Dummy
class Application < Rails::Application
CACHE_CONTROL_FIVE_MINUTES = "public, max-age=300".freeze

config.root = File.expand_path("..", __FILE__).freeze
config.eager_load = false

Expand All @@ -23,6 +25,8 @@ class Application < Rails::Application
# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr

config.static_cache_control = CACHE_CONTROL_FIVE_MINUTES

config.secret_token = "SECRET_TOKEN_IS_MIN_30_CHARS_LONG"
config.secret_key_base = "SECRET_KEY_BASE"

Expand Down
17 changes: 17 additions & 0 deletions spec/requests/assets/my-app.js_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
describe "GET assets/my-app.js" do
it "responds with the 'Cache-Control' header from Rails" do
build_ember_cli_assets

get "/assets/my-app.js"

expect(headers["Cache-Control"]).to eq(cache_for_five_minutes)
end

def build_ember_cli_assets
EmberCli["my-app"].build
end

def cache_for_five_minutes
Dummy::Application::CACHE_CONTROL_FIVE_MINUTES
end
end

0 comments on commit 4c67d23

Please sign in to comment.