Skip to content

Commit 4c67d23

Browse files
committed
Configure EmberCli::Deploy::File cache headers
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
1 parent 7685862 commit 4c67d23

File tree

5 files changed

+49
-3
lines changed

5 files changed

+49
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
master
22
------
33

4+
* `EmberCli::Deploy::File` serves assets with Rails' `static_cache_control`
5+
value. [#403]
6+
7+
[#403]: https://github.com/thoughtbot/ember-cli-rails/pull/403
8+
49
0.7.1
510
-----
611

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,22 @@ As long as your [CDN is configured to pull from your Rails application][dns-cdn]
203203

204204
### Deployment Strategies
205205

206-
By default, EmberCLI-Rails will serve the `index.html` file that `ember build`
207-
produces.
206+
By default, EmberCLI-Rails uses a file-based deployment strategy that depends on
207+
the output of `ember build`.
208+
209+
Using this deployment strategy, Rails will serve the `index.html` file and other
210+
assets that `ember build` produces.
211+
212+
These EmberCLI-generated assets are served with the same `Cache-Control` headers
213+
as Rails' other static files:
214+
215+
```rb
216+
# config/environments/production.rb
217+
Rails.application.configure do
218+
# serve static files with cache headers set to expire in 1 year
219+
config.static_cache_control = "public, max-age=31622400"
220+
end
221+
```
208222

209223
If you need to override this behavior (for instance, if you're using
210224
[`ember-cli-deploy`'s "Lightning Fast Deployment"][lightning] strategy in

lib/ember_cli/deploy/file.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def mountable?
1313
end
1414

1515
def to_rack
16-
Rack::File.new(app.dist_path.to_s)
16+
Rack::File.new(app.dist_path.to_s, rack_headers)
1717
end
1818

1919
def index_html
@@ -28,6 +28,12 @@ def index_html
2828

2929
attr_reader :app
3030

31+
def rack_headers
32+
{
33+
"Cache-Control" => Rails.configuration.static_cache_control,
34+
}
35+
end
36+
3137
def check_for_error_and_raise!
3238
app.check_for_errors!
3339

spec/dummy/application.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
module Dummy
99
class Application < Rails::Application
10+
CACHE_CONTROL_FIVE_MINUTES = "public, max-age=300".freeze
11+
1012
config.root = File.expand_path("..", __FILE__).freeze
1113
config.eager_load = false
1214

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

28+
config.static_cache_control = CACHE_CONTROL_FIVE_MINUTES
29+
2630
config.secret_token = "SECRET_TOKEN_IS_MIN_30_CHARS_LONG"
2731
config.secret_key_base = "SECRET_KEY_BASE"
2832

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
describe "GET assets/my-app.js" do
2+
it "responds with the 'Cache-Control' header from Rails" do
3+
build_ember_cli_assets
4+
5+
get "/assets/my-app.js"
6+
7+
expect(headers["Cache-Control"]).to eq(cache_for_five_minutes)
8+
end
9+
10+
def build_ember_cli_assets
11+
EmberCli["my-app"].build
12+
end
13+
14+
def cache_for_five_minutes
15+
Dummy::Application::CACHE_CONTROL_FIVE_MINUTES
16+
end
17+
end

0 commit comments

Comments
 (0)