From 6b8f63c4d525d6407eb4fef8c454cc96a3970f96 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Thu, 14 Jun 2018 14:11:31 +1200 Subject: [PATCH] Refactor redirect cache busting into middleware --- docs/en/04_Changelogs/4.2.0.md | 4 ++++ src/Control/Middleware/HTTPCacheControlMiddleware.php | 5 +++++ src/Control/RequestHandler.php | 3 --- src/Forms/Form.php | 6 +----- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/docs/en/04_Changelogs/4.2.0.md b/docs/en/04_Changelogs/4.2.0.md index d94fbc699..f942dea09 100644 --- a/docs/en/04_Changelogs/4.2.0.md +++ b/docs/en/04_Changelogs/4.2.0.md @@ -357,3 +357,7 @@ class PageController extends ContentController Note this is different from `Vary: Accept-Encoding`, which is important for compression (e.g. gzip), and usually added by other layers such as Apache's mod_gzip. + * DataObject no longer automatically invokes `HTTP::register_modification_date` + with LastModified on construction. User code should instead invoke + `HTTPCacheControlMiddleware::singleton()->registerModificationDate()` explicitly. + diff --git a/src/Control/Middleware/HTTPCacheControlMiddleware.php b/src/Control/Middleware/HTTPCacheControlMiddleware.php index 5038493a7..ea331a48a 100644 --- a/src/Control/Middleware/HTTPCacheControlMiddleware.php +++ b/src/Control/Middleware/HTTPCacheControlMiddleware.php @@ -769,5 +769,10 @@ class HTTPCacheControlMiddleware implements HTTPMiddleware, Resettable // Even if publicCache(true) is specified, errors will be uncacheable $this->disableCache(true); } + + // Don't cache redirects + if ($response->isRedirect()) { + $this->disableCache(true); + } } } diff --git a/src/Control/RequestHandler.php b/src/Control/RequestHandler.php index 5ab831c2f..369799f4c 100644 --- a/src/Control/RequestHandler.php +++ b/src/Control/RequestHandler.php @@ -657,9 +657,6 @@ class RequestHandler extends ViewableData */ public function redirectBack() { - // Don't cache the redirect back ever - HTTPCacheControlMiddleware::singleton()->disableCache(true); - // Prefer to redirect to ?BackURL, but fall back to Referer header // As a last resort redirect to base url $url = $this->getBackURL() diff --git a/src/Forms/Form.php b/src/Forms/Form.php index 186ebf659..0619f78f5 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -1812,13 +1812,9 @@ class Form extends ViewableData implements HasRequestHandler /** * Can the body of this form be cached? * - * Figure out if we can cache this form - * - forms with validation shouldn't be cached, because their error messages won't be shown - * - forms with security tokens shouldn't be cached because security tokens expire - * * @return bool */ - public function canBeCached() + protected function canBeCached() { if ($this->getSecurityToken()->isEnabled()) { return false;