Skip to content

Commit 064bacb

Browse files
committed
Use correct Cache-Tag (CDN) and X-RTD-Project header on subprojects
Currently, when accessing a subproject (eg. `edx-guide-for-students` being subproject of `edx`) we are generating `Cache-Tag: edx,edx-latest`. Then, when a build for `edx-guide-for-students` on version `latest` succeeds, we are purging the cache tags `edx-guide-for-students-latest`. This commit makes the `Cache-Tag` generated for subprojects to use the subproject's slug instead of the parent's/superproject's slug and matching the cache tag that we are purging when the build succeeds. Besides, it fixes the `X-RTD-Project` header as well, since it was using the same parent's/superproject's slug as value.
1 parent a81d82e commit 064bacb

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

readthedocs/proxito/middleware.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class ProxitoMiddleware(MiddlewareMixin):
120120
def add_proxito_headers(self, request, response):
121121
"""Add debugging headers to proxito responses."""
122122

123-
project_slug = getattr(request, 'host_project_slug', '')
123+
project_slug = getattr(request, 'path_project_slug', '')
124124
version_slug = getattr(request, 'path_version_slug', '')
125125
path = getattr(response, 'proxito_path', '')
126126

readthedocs/proxito/tests/test_headers.py

+16
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ def test_serve_headers(self):
3838
self.assertEqual(r['X-RTD-version-Method'], 'path')
3939
self.assertEqual(r['X-RTD-Path'], '/proxito/media/html/project/latest/index.html')
4040

41+
def test_subproject_serve_headers(self):
42+
r = self.client.get('/projects/subproject/en/latest/', HTTP_HOST='project.dev.readthedocs.io')
43+
self.assertEqual(r.status_code, 200)
44+
self.assertEqual(r['Cache-Tag'], 'subproject,subproject-latest')
45+
self.assertEqual(r['X-RTD-Domain'], 'project.dev.readthedocs.io')
46+
self.assertEqual(r['X-RTD-Project'], 'subproject')
47+
48+
# I think it's not accurate saying that it's `subdomain` the method
49+
# that we use to get the project slug here, since it was in fact the
50+
# URL's path but we don't have that feature built
51+
self.assertEqual(r['X-RTD-Project-Method'], 'subdomain')
52+
53+
self.assertEqual(r['X-RTD-Version'], 'latest')
54+
self.assertEqual(r['X-RTD-version-Method'], 'path')
55+
self.assertEqual(r['X-RTD-Path'], '/proxito/media/html/subproject/latest/index.html')
56+
4157
def test_404_headers(self):
4258
r = self.client.get('/foo/bar.html', HTTP_HOST='project.dev.readthedocs.io')
4359
self.assertEqual(r.status_code, 404)

readthedocs/proxito/views/utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ def _get_project_data_from_request(
9292
# * Subproject
9393
# * Translations
9494

95-
# Set the version slug on the request so we can log it in middleware
95+
# Set the project and version slug on the request so we can log it in middleware
96+
request.path_project_slug = final_project.slug
9697
request.path_version_slug = version_slug
9798

9899
return final_project, lang_slug, version_slug, filename

0 commit comments

Comments
 (0)