Skip to content

Commit 6e46d3b

Browse files
committed
Add no-cache headers to draft blog posts
Fixes T2158. Use Cache-Control headers for draft blog posts so that they are not cached (since we want changes to be reflected immediately). "Cache-Control" was used instead of Fastly's "Surrogate-Control" because the former is a common standard that is not exclusive to Fastly.
1 parent 6418dc1 commit 6e46d3b

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

blog/tests.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,62 @@ def test_user_cannot_see_unpublished_entries(self):
406406
response = self.client.get(published_url)
407407
self.assertEqual(response.status_code, 200)
408408

409+
def test_drafts_have_no_cache_headers(self):
410+
"""
411+
Test that unpublished entries have no-cache headers and that content
412+
changes are immediately visible (not cached).
413+
"""
414+
user = User.objects.create(username="staff", is_staff=True)
415+
content_type = ContentType.objects.get_for_model(Entry)
416+
change_permission = Permission.objects.get(
417+
content_type=content_type, codename="change_entry"
418+
)
419+
user.user_permissions.add(change_permission)
420+
self.client.force_login(user)
421+
422+
unpublished_entry = Entry.objects.create(
423+
pub_date=self.tomorrow,
424+
is_active=True,
425+
headline="unpublished",
426+
slug="unpublished",
427+
body="draft 1",
428+
)
429+
unpublished_url = reverse(
430+
"weblog:entry",
431+
kwargs={
432+
"year": unpublished_entry.pub_date.year,
433+
"month": unpublished_entry.pub_date.strftime("%b").lower(),
434+
"day": unpublished_entry.pub_date.day,
435+
"slug": unpublished_entry.slug,
436+
},
437+
)
438+
439+
response = self.client.get(unpublished_url)
440+
441+
self.assertEqual(response.status_code, 200)
442+
self.assertIn("Cache-Control", response.headers)
443+
self.assertEqual(response.headers["Cache-Control"], "private, no-cache")
444+
445+
def test_published_blogs_do_not_have_cache_control_headers(self):
446+
"""
447+
Test that published blog posts don't have explicit no-cache headers.
448+
"""
449+
entry = Entry.objects.create(
450+
pub_date=self.yesterday, is_active=True, headline="foo", slug="foo"
451+
)
452+
url = reverse(
453+
"weblog:entry",
454+
kwargs={
455+
"year": entry.pub_date.year,
456+
"month": entry.pub_date.strftime("%b").lower(),
457+
"day": entry.pub_date.day,
458+
"slug": entry.slug,
459+
},
460+
)
461+
response = self.client.get(url)
462+
self.assertEqual(response.status_code, 200)
463+
self.assertNotIn("Cache-Control", response.headers)
464+
409465

410466
class SitemapTests(DateTimeMixin, TestCase):
411467
def test_sitemap(self):

blog/views.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,9 @@ def get_queryset(self):
5858
return Entry.objects.all()
5959
else:
6060
return super().get_queryset()
61+
62+
def get(self, request, *args, **kwargs):
63+
response = super().get(request, *args, **kwargs)
64+
if not self.object.is_published():
65+
response["Cache-Control"] = "private, no-cache"
66+
return response

0 commit comments

Comments
 (0)