Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure EmberCli::Deploy::File cache headers #403

Merged
merged 1 commit into from
Feb 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this workaround was required, as I couldn't get ClimateControl to override an ENV variable before this class was loaded.


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