diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a22cd4f..73826ec3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ----- diff --git a/README.md b/README.md index 01bbe4b5..ee2a1b98 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/ember_cli/deploy/file.rb b/lib/ember_cli/deploy/file.rb index d402b833..68731930 100644 --- a/lib/ember_cli/deploy/file.rb +++ b/lib/ember_cli/deploy/file.rb @@ -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 @@ -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! diff --git a/spec/dummy/application.rb b/spec/dummy/application.rb index 34f5d714..f1699343 100644 --- a/spec/dummy/application.rb +++ b/spec/dummy/application.rb @@ -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 @@ -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" diff --git a/spec/requests/assets/my-app.js_spec.rb b/spec/requests/assets/my-app.js_spec.rb new file mode 100644 index 00000000..dc9656fa --- /dev/null +++ b/spec/requests/assets/my-app.js_spec.rb @@ -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