Skip to content

Commit

Permalink
NEW Add better HTTP cache-control manipulation (silverstripe#8086)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhensby authored and Damian Mooyman committed Jun 7, 2018
1 parent 1d0cffd commit 2b49540
Show file tree
Hide file tree
Showing 20 changed files with 1,295 additions and 160 deletions.
12 changes: 9 additions & 3 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ MySQLDatabase:
collation: utf8_general_ci
HTTP:
cache_control:
max-age: 0
no-cache: "true"
no-store: "true"
must-revalidate: "true"
no-transform: "true"
vary: "Cookie, X-Forwarded-Protocol, User-Agent, Accept"
vary: "X-Requested-With, X-Forwarded-Protocol"
LeftAndMain:
dependencies:
versionProvider: %$SilverStripeVersionProvider
SilverStripeVersionProvider:
modules:
silverstripe/framework: Framework
---
Only:
environment: dev
---
HTTP:
disable_http_cache: true
5 changes: 1 addition & 4 deletions api/RSSFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,7 @@ public function outputToBrowser() {
HTTP::register_etag($this->etag);
}

if(!headers_sent()) {
HTTP::add_cache_headers();
$response->addHeader("Content-Type", "application/rss+xml; charset=utf-8");
}
$response->addHeader("Content-Type", "application/rss+xml; charset=utf-8");

Config::inst()->update('SSViewer', 'source_file_comments', $prevState);

Expand Down
5 changes: 1 addition & 4 deletions control/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,7 @@ public function handleRequest(SS_HTTPRequest $request, DataModel $model) {
$response->setBody($body);
}


ContentNegotiator::process($response);
HTTP::add_cache_headers($response);

$this->popCurrent();
return $response;
}
Expand Down Expand Up @@ -502,7 +499,7 @@ public function redirect($url, $code=302) {
*/
public function redirectBack() {
// Don't cache the redirect back ever
HTTP::set_cache_age(0);
HTTPCacheControl::singleton()->disableCache(true);

$url = null;

Expand Down
9 changes: 8 additions & 1 deletion control/Director.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,14 @@ protected static function handleRequest(SS_HTTPRequest $request, Session $sessio
} catch(SS_HTTPResponse_Exception $responseException) {
$result = $responseException->getResponse();
}
if(!is_object($result) || $result instanceof SS_HTTPResponse) return $result;
// Ensure cache headers are added
if ($result instanceof SS_HTTPResponse) {
HTTP::add_cache_headers($result);
return $result;
}
if(!is_object($result)) {
return $result;
}

user_error("Bad result from url " . $request->getURL() . " handled by " .
get_class($controllerObj)." controller: ".get_class($result), E_USER_WARNING);
Expand Down
3 changes: 3 additions & 0 deletions control/FlushRequestFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public function preRequest(SS_HTTPRequest $request, Session $session, DataModel
}

public function postRequest(SS_HTTPRequest $request, SS_HTTPResponse $response, DataModel $model) {
if(array_key_exists('flush', $request->getVars())) {
HTTPCacheControl::singleton()->disableCache(true);
}
return true;
}

Expand Down

0 comments on commit 2b49540

Please sign in to comment.