feat: allow section-based Surrogate-Key to purge chunks#2851
feat: allow section-based Surrogate-Key to purge chunks#2851JacobCoffee merged 7 commits intomainfrom
Conversation
… cause The “Please turn on JavaScript” notice can be shown even when JavaScript is enabled, e.g. due to intermittent asset loading or caching issues (python#2324, python#2851). This change updates the copy to explain why the notice is shown and to list possible causes without asserting that JavaScript is disabled on the client.
974c458 to
f1d6aea
Compare
f3b96dd to
f0ebb7a
Compare
There was a problem hiding this comment.
Pull request overview
This pull request implements section-based surrogate key purging for Fastly CDN, enabling more efficient cache invalidation by purging entire sections (e.g., /downloads/*) in a single API request rather than individual URLs. This significantly simplifies cache management when deploying changes that affect multiple pages.
Changes:
- Added
purge_surrogate_key()utility function to purge Fastly cache by surrogate key - Enhanced
GlobalSurrogateKeymiddleware to add section-based surrogate keys derived from URL paths - Simplified Release post_save signal to use surrogate key purging instead of individual URL purges
- Added GitHub Actions workflow for manual/automated cache purging via surrogate keys
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
pydotorg/middleware.py |
Enhanced middleware to extract and append section keys (e.g., "downloads", "events") from URL paths to the Surrogate-Key header |
pydotorg/tests/test_middleware.py |
Added comprehensive tests for section key extraction and Surrogate-Key header generation |
fastly/utils.py |
Added purge_surrogate_key() function to purge all cached content with a specific surrogate key via Fastly API |
apps/downloads/models.py |
Refactored purge_fastly_download_pages to use surrogate key purging instead of individual URL purges |
pydotorg/settings/base.py |
Added FASTLY_SERVICE_ID configuration required for surrogate key API endpoint |
.github/workflows/purge-cache.yml |
Created workflow to manually/automatically purge cache sections on template/static file changes |
Comments suppressed due to low confidence (1)
fastly/utils.py:49
- The new
purge_surrogate_keyfunction lacks test coverage. The codebase has test patterns for mocking external API calls (see apps/downloads/tests/test_template_tags.py for examples using @mock.patch on requests.get). Consider adding tests to verify that purge_surrogate_key correctly calls the Fastly API with the right parameters, handles missing configuration, and respects the DEBUG setting.
def purge_surrogate_key(key):
"""Purge all Fastly cached content tagged with a surrogate key.
Common keys (set by GlobalSurrogateKey middleware):
- 'pydotorg-app': Purges entire site
- 'downloads': Purges all /downloads/* pages
- 'events': Purges all /events/* pages
- 'sponsors': Purges all /sponsors/* pages
- etc. (first path segment becomes the surrogate key)
Returns the response from Fastly API, or None if not configured.
"""
if settings.DEBUG:
return None
api_key = getattr(settings, "FASTLY_API_KEY", None)
service_id = getattr(settings, "FASTLY_SERVICE_ID", None)
if not api_key or not service_id:
return None
return requests.post(
f"https://api.fastly.com/service/{service_id}/purge/{key}",
headers={"Fastly-Key": api_key},
timeout=30,
)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…hings without nukiung the whole cache
f0ebb7a to
d02f8f8
Compare
d02f8f8 to
7d5ba7b
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Prevents silent no-op regression when surrogate key purging is not available. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Description
https://www.fastly.com/documentation/guides/full-site-delivery/purging/working-with-surrogate-keys/
When working with release changes lately we've needed to purge all releases because cache is wonky with stale CSS.
fastly ui and api doesnt seem to let me just do "root path, also purge all children" but maybe im missing it and we dont need this
Had to do something like this (which notably still missing pages like /downloads/android/ and others..):
Details
against
releases.txt:but ideally we just purge all of downloads/ when needed