Skip to content

Commit ab33fb4

Browse files
committed
Set Cache-Control: no-cache if cache buster check fails
If an asset URL fetch fails due to version skew across multiple servers in a deployment, prevent the 4xx response from being cached by the CDN or browser. The proper solution is for the downstream app to either deploy assets ahead of the app deployment, or use sticky sessions. This change reduces the downside if eg. stickiness fails for any reason. Fixes #27
1 parent 31b37ec commit ab33fb4

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/h_assets/view.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ def wrapper(context, request):
1616
if request.query_string and not environment.check_cache_buster(
1717
request.path, request.query_string
1818
):
19-
return HTTPNotFound()
19+
response = HTTPNotFound()
20+
21+
# Disable downstream caching of failed responses, in case this
22+
# happened due to version skew during a deployment. See
23+
# https://github.com/hypothesis/h-assets/issues/27.
24+
response.cache_control.no_cache = True
25+
26+
return response
2027

2128
return static(context, request)
2229

tests/unit/h_assets/view_test.py

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ def test_it_returns_404_if_cache_buster_invalid(self, environment, static_view):
5050
static_view.return_value.assert_not_called()
5151
assert isinstance(response, HTTPNotFound)
5252

53+
# Returns "*" though set to `True`
54+
assert response.cache_control.no_cache # pylint: disable=no-member
55+
5356
@pytest.fixture
5457
def environment(self):
5558
return create_autospec(Environment, instance=True, spec_set=True)

0 commit comments

Comments
 (0)