提交 2d04bdd8 编写于 作者: G Godfrey Chan

Merge pull request #18100 from chancancode/serve_static_files

Allow static asset serving from env variable (enhanced!)
Conflicts:
	railties/CHANGELOG.md
上级 352680c1
......@@ -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
......
......@@ -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:
......
......@@ -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`**
......
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'
......@@ -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
......@@ -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 = {}
......@@ -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
......
......@@ -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
......
......@@ -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.
......
......@@ -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.
......
......@@ -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!
......
......@@ -41,7 +41,7 @@ def app
def setup
build_app
boot_rails
FileUtils.rm_rf("#{app_path}/config/environments")
supress_default_config
end
def teardown
......@@ -49,6 +49,15 @@ def teardown
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"
......@@ -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'
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册