diff --git a/CHANGELOG.md b/CHANGELOG.md index 012ea9475..e5f258008 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # v1.6.17 ## mm/dd/2019 +1. [](#new) + * Added working ETag (304 Not Modified) support based on the final rendered HTML 1. [](#improved) * Safer file handling + customizable null char replacement in `CsvFormatter::decode()` * Change of Behavior: `Inflector::hyphenize` will now automatically trim dashes at beginning and end of a string. diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index 009d57de5..9cb74cc18 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -247,9 +247,21 @@ class Grav extends Container $collection = new RequestHandler($this->middleware, $default, $container); $response = $collection->handle($this['request']); + $body = $response->getBody(); + + // Handle ETag and If-None-Match headers. + if ($response->getHeaderLine('ETag') === '1') { + $etag = md5($body); + $response = $response->withHeader('ETag', $etag); + + if ($this['request']->getHeaderLine('If-None-Match') === $etag) { + $response = $response->withStatus(304); + $body = ''; + } + } $this->header($response); - echo $response->getBody(); + echo $body; $debugger->render(); diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index f59665ff5..199660852 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -529,9 +529,9 @@ class Page implements PageInterface $headers['Last-Modified'] = $last_modified_date; } - // Calculate ETag based on the raw file + // Ask Grav to calculate ETag from the final content. if ($this->eTag()) { - $headers['ETag'] = '"' . md5($this->raw() . $this->modified()).'"'; + $headers['ETag'] = '1'; } // Set Vary: Accept-Encoding header