Skip to content

Commit

Permalink
Allow skipping the browser cache
Browse files Browse the repository at this point in the history
  • Loading branch information
grcooper committed Sep 7, 2023
1 parent 1e1b54e commit 867f32f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/response_bank/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def force_refill_cache?
params[:fill_cache] == "true"
end

def skip_browser_cache?
false
end

def serve_unversioned_cacheable_entry?
false
end
Expand Down Expand Up @@ -48,6 +52,7 @@ def response_cache(key_data = nil, version_data = nil, &block)
cache_age_tolerance: cache_age_tolerance_in_seconds,
serve_unversioned: serve_unversioned_cacheable_entry?,
force_refill_cache: force_refill_cache?,
skip_browser_cache: skip_browser_cache?,
headers: response.headers,
&block
)
Expand Down
8 changes: 6 additions & 2 deletions lib/response_bank/response_cache_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def initialize(
serve_unversioned:,
headers:,
force_refill_cache: false,
skip_browser_cache: false,
cache_store: ResponseBank.cache_store,
&block
)
Expand All @@ -25,6 +26,7 @@ def initialize(

@serve_unversioned = serve_unversioned
@force_refill_cache = force_refill_cache
@skip_browser_cache = skip_browser_cache
@cache_store = cache_store
@headers = headers || {}
@key_schema_version = @env.key?('cacheable.key_version') ? @env.key['cacheable.key_version'] : CACHE_KEY_SCHEMA_VERSION
Expand Down Expand Up @@ -81,8 +83,10 @@ def cacheable_info_dump

def try_to_serve_from_cache
# Etag
response = serve_from_browser_cache(entity_tag_hash, @env['HTTP_IF_NONE_MATCH'])
return response if response
unless @skip_browser_cache
response = serve_from_browser_cache(entity_tag_hash, @env['HTTP_IF_NONE_MATCH'])
return response if response
end

response = serve_from_cache(cache_key_hash, @serve_unversioned ? "*" : entity_tag_hash, @cache_age_tolerance)
return response if response
Expand Down
12 changes: 12 additions & 0 deletions test/response_cache_handler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def handler
cache_store: @cache_store,
env: controller.request.env,
force_refill_cache: controller.send(:force_refill_cache?),
skip_browser_cache: controller.send(:skip_browser_cache?),
serve_unversioned: controller.send(:serve_unversioned_cacheable_entry?),
cache_age_tolerance: controller.send(:cache_age_tolerance_in_seconds),
headers: controller.response.headers,
Expand Down Expand Up @@ -205,6 +206,17 @@ def test_force_refill_cache
assert_equal('dynamic output', body)
end

def test_skip_browser_cache_never_loads_from_browser
@controller.stubs(serve_from_browser_cache?: true)
@controller.expects(:serve_from_browser_cache).never
controller.request.env['HTTP_IF_NONE_MATCH'] = handler.entity_tag_hash
@cache_store.expects(:read).with(handler.cache_key_hash, raw: true).returns(page_cache_entry)

status, _, body = handler.run!
assert_equal(200, status)
assert_equal('dynamic output', body)
end

def test_serve_unversioned_cacheable_entry
assert(@controller.respond_to?(:serve_unversioned_cacheable_entry?, true))
@controller.expects(:serve_unversioned_cacheable_entry?).returns(true).times(1)
Expand Down

0 comments on commit 867f32f

Please sign in to comment.