Invert precedence of content in ActionDispatch::Static
This commit inverts the precedence in ActionDispatch::Static so that dynamic content will be served before static content. This is so that precompiled assets do not inadvertently get included when running in development mode - it should have no effect in production where static files are usually handled by the web server. Closes #6421
This commit is contained in:
parent
5a8f25f003
commit
c59734f756
@ -1,4 +1,11 @@
|
|||||||
## Rails 4.0.0 (unreleased) ##
|
## Rails 4.0.0 (unreleased) ##
|
||||||
|
|
||||||
|
* Invert precedence in `ActionDispatch::Static` so that dynamic content is preferred.
|
||||||
|
This prevents precompiled assets inadvertently being included twice when running
|
||||||
|
in development mode. Fixes #6421
|
||||||
|
|
||||||
|
*Andrew White*
|
||||||
|
|
||||||
* Add :if / :unless conditions to fragment cache:
|
* Add :if / :unless conditions to fragment cache:
|
||||||
|
|
||||||
<%= cache @model, if: some_condition(@model) do %>
|
<%= cache @model, if: some_condition(@model) do %>
|
||||||
|
@ -51,16 +51,20 @@ def initialize(app, path, cache_control=nil)
|
|||||||
end
|
end
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
case env['REQUEST_METHOD']
|
path = env['PATH_INFO'].chomp('/')
|
||||||
when 'GET', 'HEAD'
|
response = @app.call(env)
|
||||||
path = env['PATH_INFO'].chomp('/')
|
|
||||||
if match = @file_handler.match?(path)
|
if response[1]['X-Cascade'] == 'pass'
|
||||||
env["PATH_INFO"] = match
|
case env['REQUEST_METHOD']
|
||||||
return @file_handler.call(env)
|
when 'GET', 'HEAD'
|
||||||
|
if match = @file_handler.match?(path)
|
||||||
|
env["PATH_INFO"] = match
|
||||||
|
return @file_handler.call(env)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@app.call(env)
|
response
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -4,11 +4,19 @@
|
|||||||
|
|
||||||
module StaticTests
|
module StaticTests
|
||||||
def test_serves_dynamic_content
|
def test_serves_dynamic_content
|
||||||
|
dummy_app = lambda { |env| [200, {"Content-Type" => "text/plain"}, ["Hello, World!"]] }
|
||||||
|
@app = ActionDispatch::Static.new(dummy_app, "#{FIXTURE_LOAD_PATH}/public", "public, max-age=60")
|
||||||
assert_equal "Hello, World!", get("/nofile").body
|
assert_equal "Hello, World!", get("/nofile").body
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_dynamic_content_has_precedence_over_static_files
|
||||||
|
dummy_app = lambda { |env| [200, {"Content-Type" => "text/html"}, ["/foo/baz.html"]] }
|
||||||
|
@app = ActionDispatch::Static.new(dummy_app, "#{FIXTURE_LOAD_PATH}/public", "public, max-age=60")
|
||||||
|
assert_html "/foo/baz.html", get("/foo/bar.html")
|
||||||
|
end
|
||||||
|
|
||||||
def test_handles_urls_with_bad_encoding
|
def test_handles_urls_with_bad_encoding
|
||||||
assert_equal "Hello, World!", get("/doorkeeper%E3E4").body
|
assert_equal "", get("/doorkeeper%E3E4").body
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_sets_cache_control
|
def test_sets_cache_control
|
||||||
@ -40,7 +48,6 @@ def test_served_static_file_with_non_english_filename
|
|||||||
assert_html "means hello in Japanese\n", get("/foo/#{Rack::Utils.escape("こんにちは.html")}")
|
assert_html "means hello in Japanese\n", get("/foo/#{Rack::Utils.escape("こんにちは.html")}")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def test_serves_static_file_with_exclamation_mark_in_filename
|
def test_serves_static_file_with_exclamation_mark_in_filename
|
||||||
with_static_file "/foo/foo!bar.html" do |file|
|
with_static_file "/foo/foo!bar.html" do |file|
|
||||||
assert_html file, get("/foo/foo%21bar.html")
|
assert_html file, get("/foo/foo%21bar.html")
|
||||||
@ -142,9 +149,7 @@ def with_static_file(file)
|
|||||||
end
|
end
|
||||||
|
|
||||||
class StaticTest < ActiveSupport::TestCase
|
class StaticTest < ActiveSupport::TestCase
|
||||||
DummyApp = lambda { |env|
|
DummyApp = lambda { |env| [404, {"X-Cascade" => "pass"}, []] }
|
||||||
[200, {"Content-Type" => "text/plain"}, ["Hello, World!"]]
|
|
||||||
}
|
|
||||||
App = ActionDispatch::Static.new(DummyApp, "#{FIXTURE_LOAD_PATH}/public", "public, max-age=60")
|
App = ActionDispatch::Static.new(DummyApp, "#{FIXTURE_LOAD_PATH}/public", "public, max-age=60")
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
|
@ -268,5 +268,28 @@ def index
|
|||||||
get '/yazilar'
|
get '/yazilar'
|
||||||
assert_equal 200, last_response.status
|
assert_equal 200, last_response.status
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test 'routes take precedence over static files' do
|
||||||
|
app('development')
|
||||||
|
|
||||||
|
app_file 'config/routes.rb', <<-RUBY
|
||||||
|
AppTemplate::Application.routes.draw do
|
||||||
|
get 'foo', to: 'foo#index'
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
app_file 'public/foo.json', '{"foo":"bar"}'
|
||||||
|
|
||||||
|
controller :foo, <<-RUBY
|
||||||
|
class FooController < ApplicationController
|
||||||
|
def index
|
||||||
|
render json: { foo: 'baz' }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
get '/foo.json'
|
||||||
|
assert_equal '{"foo":"baz"}', last_response.body
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user