Skip to content

Commit a12952c

Browse files
authored
Proxito: add Cache-Control HTTP headers (#12348)
Always add `Cache-Control` to external domains. Closes #12244
1 parent 178c73b commit a12952c

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

dockerfiles/nginx/proxito.conf.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ server {
7777
add_header X-RTD-Resolver-Filename $rtd_resolver_filename always;
7878
set $cdn_cache_control $upstream_http_cdn_cache_control;
7979
add_header CDN-Cache-Control $cdn_cache_control always;
80+
set $cache_control $upstream_http_cache_control;
81+
add_header Cache-Control $cache_control always;
8082
set $cache_tag $upstream_http_cache_tag;
8183
add_header Cache-Tag $cache_tag always;
8284
set $referrer_policy $upstream_http_referrer_policy;

readthedocs/proxito/middleware.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ def add_hsts_headers(self, request, response):
159159
response["Strict-Transport-Security"] = "; ".join(hsts_header_values)
160160

161161
def add_cache_headers(self, request, response):
162+
"""Add `Cache-Control: no-cache` header (browser level) for external versions."""
163+
unresolved_domain = request.unresolved_domain
164+
if unresolved_domain and unresolved_domain.is_from_external_domain:
165+
response["Cache-Control"] = "no-cache"
166+
167+
def add_cdn_cache_headers(self, request, response):
162168
"""
163169
Add Cache-Control headers.
164170
@@ -360,6 +366,7 @@ def add_resolver_headers(self, request, response):
360366
def process_response(self, request, response): # noqa
361367
self.add_proxito_headers(request, response)
362368
self.add_cache_headers(request, response)
369+
self.add_cdn_cache_headers(request, response)
363370
self.add_hsts_headers(request, response)
364371
self.add_user_headers(request, response)
365372
self.add_hosting_integrations_headers(request, response)

readthedocs/proxito/tests/test_headers.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,27 @@ def test_cache_headers_robots_txt_with_private_projects_allowed(self):
372372
self.assertEqual(r.status_code, 200)
373373
self.assertEqual(r["CDN-Cache-Control"], "public")
374374
self.assertEqual(r["Cache-Tag"], "project,project:sitemap.xml")
375+
376+
def test_cache_headers_at_browser_level_on_external_domain(self):
377+
r = self.client.get(
378+
"/en/latest/", secure=True, headers={"host": "project.dev.readthedocs.io"}
379+
)
380+
self.assertEqual(r.status_code, 200)
381+
self.assertNotIn("no-cache", r.headers)
382+
383+
get(
384+
Version,
385+
project=self.project,
386+
slug="111",
387+
active=True,
388+
privacy_level=PUBLIC,
389+
type=EXTERNAL,
390+
)
391+
392+
r = self.client.get(
393+
"/en/111/",
394+
secure=True,
395+
headers={"host": "project--111.dev.readthedocs.build"},
396+
)
397+
self.assertEqual(r.status_code, 200)
398+
self.assertEqual(r["Cache-Control"], "no-cache")

0 commit comments

Comments
 (0)