1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #33134 from dasch/dasch/extra-cache-controls

Add support for more HTTP cache controls
This commit is contained in:
Eileen M. Uchitelle 2018-06-19 08:52:16 -04:00 committed by GitHub
commit 2be5ef6a1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 2 deletions

View file

@ -235,7 +235,9 @@ module ActionController
response.cache_control.merge!( response.cache_control.merge!(
max_age: seconds, max_age: seconds,
public: options.delete(:public), public: options.delete(:public),
must_revalidate: options.delete(:must_revalidate) must_revalidate: options.delete(:must_revalidate),
stale_while_revalidate: options.delete(:stale_while_revalidate),
stale_if_error: options.delete(:stale_if_error),
) )
options.delete(:private) options.delete(:private)

View file

@ -202,13 +202,17 @@ module ActionDispatch
self._cache_control = _cache_control + ", #{control[:extras].join(', ')}" self._cache_control = _cache_control + ", #{control[:extras].join(', ')}"
end end
else else
extras = control[:extras] extras = control[:extras]
max_age = control[:max_age] max_age = control[:max_age]
stale_while_revalidate = control[:stale_while_revalidate]
stale_if_error = control[:stale_if_error]
options = [] options = []
options << "max-age=#{max_age.to_i}" if max_age options << "max-age=#{max_age.to_i}" if max_age
options << (control[:public] ? PUBLIC : PRIVATE) options << (control[:public] ? PUBLIC : PRIVATE)
options << MUST_REVALIDATE if control[:must_revalidate] options << MUST_REVALIDATE if control[:must_revalidate]
options << "stale-while-revalidate=#{stale_while_revalidate.to_i}" if stale_while_revalidate
options << "stale-if-error=#{stale_if_error.to_i}" if stale_if_error
options.concat(extras) if extras options.concat(extras) if extras
self._cache_control = options.join(", ") self._cache_control = options.join(", ")

View file

@ -141,6 +141,16 @@ class TestController < ActionController::Base
render action: "hello_world" render action: "hello_world"
end end
def conditional_hello_with_expires_in_with_stale_while_revalidate
expires_in 1.minute, public: true, stale_while_revalidate: 5.minutes
render action: "hello_world"
end
def conditional_hello_with_expires_in_with_stale_if_error
expires_in 1.minute, public: true, stale_if_error: 5.minutes
render action: "hello_world"
end
def conditional_hello_with_expires_in_with_public_with_more_keys def conditional_hello_with_expires_in_with_public_with_more_keys
expires_in 1.minute, :public => true, "s-maxage" => 5.hours expires_in 1.minute, :public => true, "s-maxage" => 5.hours
render action: "hello_world" render action: "hello_world"
@ -358,6 +368,16 @@ class ExpiresInRenderTest < ActionController::TestCase
assert_equal "max-age=60, public, must-revalidate", @response.headers["Cache-Control"] assert_equal "max-age=60, public, must-revalidate", @response.headers["Cache-Control"]
end end
def test_expires_in_header_with_stale_while_revalidate
get :conditional_hello_with_expires_in_with_stale_while_revalidate
assert_equal "max-age=60, public, stale-while-revalidate=300", @response.headers["Cache-Control"]
end
def test_expires_in_header_with_stale_if_error
get :conditional_hello_with_expires_in_with_stale_if_error
assert_equal "max-age=60, public, stale-if-error=300", @response.headers["Cache-Control"]
end
def test_expires_in_header_with_additional_headers def test_expires_in_header_with_additional_headers
get :conditional_hello_with_expires_in_with_public_with_more_keys get :conditional_hello_with_expires_in_with_public_with_more_keys
assert_equal "max-age=60, public, s-maxage=18000", @response.headers["Cache-Control"] assert_equal "max-age=60, public, s-maxage=18000", @response.headers["Cache-Control"]