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

Allow static asset serving from env variable (enhanced!) #18100

Merged
merged 2 commits into from Dec 19, 2014
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions guides/source/asset_pipeline.md
Expand Up @@ -167,9 +167,8 @@ directory. Files in this directory are served by the Sprockets middleware.

Assets can still be placed in the `public` hierarchy. Any assets under `public`
will be served as static files by the application or web server when
`config.serve_static_assets` is set to true. You should use
`app/assets` for files that must undergo some pre-processing before they are
served.
`config.serve_static_files` is set to true. You should use `app/assets` for
files that must undergo some pre-processing before they are served.

In production, Rails precompiles these files to `public/assets` by default. The
precompiled copies are then served as static assets by the web server. The files
Expand Down
2 changes: 1 addition & 1 deletion guides/source/configuring.md
Expand Up @@ -120,7 +120,7 @@ numbers. New applications filter out passwords by adding the following `config.f

* `secrets.secret_key_base` is used for specifying a key which allows sessions for the application to be verified against a known secure key to prevent tampering. Applications get `secrets.secret_key_base` initialized to a random key present in `config/secrets.yml`.

* `config.serve_static_assets` configures Rails itself to serve static assets. Defaults to true, but in the production environment is turned off as the server software (e.g. NGINX or Apache) used to run the application should serve static assets instead. Unlike the default setting set this to true when running (absolutely not recommended!) or testing your app in production mode using WEBrick. Otherwise you won't be able use page caching and requests for files that exist regularly under the public directory will anyway hit your Rails app.
* `config.serve_static_files` configures Rails itself to serve static files. Defaults to true, but in the production environment is turned off as the server software (e.g. NGINX or Apache) used to run the application should serve static assets instead. Unlike the default setting set this to true when running (absolutely not recommended!) or testing your app in production mode using WEBrick. Otherwise you won't be able use page caching and requests for files that exist regularly under the public directory will anyway hit your Rails app.

* `config.session_store` is usually set up in `config/initializers/session_store.rb` and specifies what class to use to store the session. Possible values are `:cookie_store` which is the default, `:mem_cache_store`, and `:disabled`. The last one tells Rails not to deal with sessions. Custom session stores can also be specified:

Expand Down
2 changes: 1 addition & 1 deletion guides/source/rails_on_rack.md
Expand Up @@ -233,7 +233,7 @@ Much of Action Controller's functionality is implemented as Middlewares. The fol

**`ActionDispatch::Static`**

* Used to serve static assets. Disabled if `config.serve_static_assets` is `false`.
* Used to serve static files. Disabled if `config.serve_static_files` is `false`.

**`Rack::Lock`**

Expand Down
11 changes: 11 additions & 0 deletions railties/CHANGELOG.md
@@ -1,3 +1,14 @@
* Deprecate `config.serve_static_assets` in favor of `config.serve_public_assets`

Choose a reason for hiding this comment

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

I believe this should read Deprecate config.serve_static_assetsin favor ofconfig.serve_static_files`` (serve_static_files vs. `serve_public_assets`).

Copy link
Member Author

Choose a reason for hiding this comment

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

You are totally right, thanks for catching the mistake!

to clarify that the option is unrelated to the asset pipeline.

*Godfrey Chan*

* `config.serve_static_assets` can now be set from an environment variable in

Choose a reason for hiding this comment

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

Again, this should probably be config.serve_static_files, as the other has been deprecated.

production mode. The feature remains off by default, but can be enabled by
setting `RAILS_SERVE_STATIC_FILES` to a non-empty string at boot time.

*Richard Schneeman*, *Godfrey Chan*

* Generated migrations add the appropriate foreign key constraints to
references.

Expand Down
25 changes: 23 additions & 2 deletions railties/lib/rails/application/configuration.rb
@@ -1,5 +1,7 @@
require 'active_support/core_ext/kernel/reporting'
require 'active_support/core_ext/string/filters'
require 'active_support/file_update_checker'
require 'active_support/deprecation'
require 'rails/engine/configuration'
require 'rails/source_annotation_extractor'

Expand All @@ -11,7 +13,7 @@ class Configuration < ::Rails::Engine::Configuration
:eager_load, :exceptions_app, :file_watcher, :filter_parameters,
:force_ssl, :helpers_paths, :logger, :log_formatter, :log_tags,
:railties_order, :relative_url_root, :secret_key_base, :secret_token,
:serve_static_assets, :ssl_options, :static_cache_control, :session_options,
:serve_static_files, :ssl_options, :static_cache_control, :session_options,
:time_zone, :reload_classes_only_on_change,
:beginning_of_week, :filter_redirect, :x

Expand All @@ -25,7 +27,7 @@ def initialize(*)
@filter_parameters = []
@filter_redirect = []
@helpers_paths = []
@serve_static_assets = true
@serve_static_files = true
@static_cache_control = nil
@force_ssl = false
@ssl_options = {}
Expand Down Expand Up @@ -139,6 +141,25 @@ def colorize_logging=(val)
self.generators.colorize_logging = val
end

# :nodoc:
SERVE_STATIC_ASSETS_DEPRECATION_MESSAGE = <<-MSG.squish
The configuration option `config.serve_static_assets` has been renamed
to `config.serve_static_files` to clarify its role (it merely enables
serving everything in the `public` folder and is unrelated to the asset
pipeline). The `serve_static_assets` alias will be removed in Rails 5.0.
Please migrate your configuration files accordingly.
MSG

def serve_static_assets
ActiveSupport::Deprecation.warn SERVE_STATIC_ASSETS_DEPRECATION_MESSAGE
serve_static_files
end

def serve_static_assets=(value)
ActiveSupport::Deprecation.warn SERVE_STATIC_ASSETS_DEPRECATION_MESSAGE
self.serve_static_files = value
end

def session_store(*args)
if args.empty?
case @session_store
Expand Down
2 changes: 1 addition & 1 deletion railties/lib/rails/application/default_middleware_stack.rb
Expand Up @@ -17,7 +17,7 @@ def build_stack

middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header

if config.serve_static_assets
if config.serve_static_files
middleware.use ::ActionDispatch::Static, paths["public"].first, config.static_cache_control
end

Expand Down
Expand Up @@ -20,8 +20,9 @@ Rails.application.configure do
# NGINX, varnish or squid.
# config.action_dispatch.rack_cache = true

# Disable Rails's static asset server (Apache or NGINX will already do this).
config.serve_static_assets = false
# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?

<%- unless options.skip_sprockets? -%>
# Compress JavaScripts and CSS.
Expand Down
Expand Up @@ -12,8 +12,8 @@ Rails.application.configure do
# preloads Rails for running tests, you may have to set it to true.
config.eager_load = false

# Configure static asset server for tests with Cache-Control for performance.
config.serve_static_assets = true
# Configure static file server for tests with Cache-Control for performance.
config.serve_static_files = true
config.static_cache_control = 'public, max-age=3600'

# Show full error reports and disable caching.
Expand Down
2 changes: 1 addition & 1 deletion railties/test/application/assets_test.rb
Expand Up @@ -225,7 +225,7 @@ class User < ActiveRecord::Base; raise 'should not be reached'; end

test "assets do not require any assets group gem when manifest file is present" do
app_file "app/assets/javascripts/application.js", "alert();"
add_to_env_config "production", "config.serve_static_assets = true"
add_to_env_config "production", "config.serve_static_files = true"

ENV["RAILS_ENV"] = "production"
precompile!
Expand Down
58 changes: 57 additions & 1 deletion railties/test/application/configuration_test.rb
Expand Up @@ -41,14 +41,23 @@ def app
def setup
build_app
boot_rails
FileUtils.rm_rf("#{app_path}/config/environments")
supress_default_config
end

def teardown
teardown_app
FileUtils.rm_rf(new_app) if File.directory?(new_app)
end

def supress_default_config
FileUtils.mv("#{app_path}/config/environments", "#{app_path}/config/__environments__")
end

def restore_default_config
FileUtils.rm_rf("#{app_path}/config/environments")
FileUtils.mv("#{app_path}/config/__environments__", "#{app_path}/config/environments")
end

test "Rails.env does not set the RAILS_ENV environment variable which would leak out into rake tasks" do
require "rails"

Expand Down Expand Up @@ -280,6 +289,53 @@ def assert_utf8
assert_equal Pathname.new(app_path).join("somewhere"), Rails.public_path
end

test "In production mode, config.serve_static_files is off by default" do
restore_default_config

with_rails_env "production" do
require "#{app_path}/config/environment"
assert_not app.config.serve_static_files
end
end

test "In production mode, config.serve_static_files is enabled when RAILS_SERVE_STATIC_FILES is set" do
restore_default_config

with_rails_env "production" do
switch_env "RAILS_SERVE_STATIC_FILES", "1" do
require "#{app_path}/config/environment"
assert app.config.serve_static_files
end
end
end

test "In production mode, config.serve_static_files is disabled when RAILS_SERVE_STATIC_FILES is blank" do
restore_default_config

with_rails_env "production" do
switch_env "RAILS_SERVE_STATIC_FILES", " " do
require "#{app_path}/config/environment"
assert_not app.config.serve_static_files
end
end
end

test "config.serve_static_assets is deprecated" do
require "#{app_path}/config/application"

assert_deprecated(/serve_static_assets/) do
app.config.serve_static_assets = false
end

assert_not app.config.serve_static_files
assert_deprecated(/serve_static_assets/) { assert_not app.config.serve_static_assets }

app.config.serve_static_files = true

assert app.config.serve_static_files
assert_deprecated(/serve_static_assets/) { assert app.config.serve_static_assets }
end

test "Use key_generator when secret_key_base is set" do
make_basic_app do |app|
app.secrets.secret_key_base = 'b3c631c314c0bbca50c1b2843150fe33'
Expand Down
2 changes: 1 addition & 1 deletion railties/test/application/middleware/sendfile_test.rb
Expand Up @@ -61,7 +61,7 @@ def index
test "files handled by ActionDispatch::Static are handled by Rack::Sendfile" do
make_basic_app do |app|
app.config.action_dispatch.x_sendfile_header = 'X-Sendfile'
app.config.serve_static_assets = true
app.config.serve_static_files = true
app.paths["public"] = File.join(rails_root, "public")
end

Expand Down
4 changes: 2 additions & 2 deletions railties/test/application/middleware_test.rb
Expand Up @@ -113,8 +113,8 @@ def app
assert !middleware.include?("Rack::Lock")
end

test "removes static asset server if serve_static_assets is disabled" do
add_to_config "config.serve_static_assets = false"
test "removes static asset server if serve_static_files is disabled" do
add_to_config "config.serve_static_files = false"
boot!
assert !middleware.include?("ActionDispatch::Static")
end
Expand Down
2 changes: 1 addition & 1 deletion railties/test/railties/engine_test.rb
Expand Up @@ -1210,7 +1210,7 @@ def foo

test "engine can be properly mounted at root" do
add_to_config("config.action_dispatch.show_exceptions = false")
add_to_config("config.serve_static_assets = false")
add_to_config("config.serve_static_files = false")

@plugin.write "lib/bukkits.rb", <<-RUBY
module Bukkits
Expand Down