From 13341c323d43eb47562d8ae72eae730e3e0e2cee Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Wed, 26 Jul 2017 12:33:35 -0600 Subject: [PATCH] Added cache-control property #1591 --- CHANGELOG.md | 6 ++++++ system/blueprints/config/system.yaml | 6 ++++++ system/config/system.yaml | 1 + system/src/Grav/Common/Grav.php | 11 ++++++++++- system/src/Grav/Common/Page/Page.php | 20 ++++++++++++++++++++ 5 files changed, 43 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eefb41e0e..dd094ed19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# v1.3.2 +## 07/xx/2017 + +1. [](#new) + * Added a new `cache-control` system and page level property [#1591](https://github.com/getgrav/grav/issues/1591) + # v1.3.1 ## 07/19/2017 diff --git a/system/blueprints/config/system.yaml b/system/blueprints/config/system.yaml index c9b959e86..4021255f3 100644 --- a/system/blueprints/config/system.yaml +++ b/system/blueprints/config/system.yaml @@ -377,6 +377,12 @@ form: validate: type: number min: 1 + pages.cache_control: + type: text + size: medium + label: PLUGIN_ADMIN.CACHE_CONTROL + help: PLUGIN_ADMIN.CACHE_CONTROL_HELP + placeholder: 'e.g. public, max-age=31536000' pages.last_modified: type: toggle label: PLUGIN_ADMIN.LAST_MODIFIED diff --git a/system/config/system.yaml b/system/config/system.yaml index 5d6ca443f..9f25ff9b6 100644 --- a/system/config/system.yaml +++ b/system/config/system.yaml @@ -53,6 +53,7 @@ pages: types: [txt,xml,html,htm,json,rss,atom] # list of valid page types append_url_extension: '' # Append page's extension in Page urls (e.g. '.html' results in /path/page.html) expires: 604800 # Page expires time in seconds (604800 seconds = 7 days) + cache_control: # Can be blank for no setting, or a valid `cache-control` text value last_modified: false # Set the last modified date header based on file modification timestamp etag: false # Set the etag header tag vary_accept_encoding: false # Add `Vary: Accept-Encoding` header diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index a260ec95f..ef2125e33 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -227,15 +227,24 @@ public function header() header('Content-type: ' . Utils::getMimeByExtension($format, 'text/html')); + $cache_control = $page->cacheControl(); + // Calculate Expires Headers if set to > 0 $expires = $page->expires(); if ($expires > 0) { $expires_date = gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT'; - header('Cache-Control: max-age=' . $expires); + if (!$cache_control) { + header('Cache-Control: max-age=' . $expires); + } header('Expires: ' . $expires_date); } + // Set cache-control header + if ($cache_control) { + header('Cache-Control: ' . strtolower($cache_control)); + } + // Set the last modified time if ($page->lastModified()) { $last_modified_date = gmdate('D, d M Y H:i:s', $page->modified()) . ' GMT'; diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index 164ec8792..2395697ba 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -42,6 +42,7 @@ class Page protected $parent; protected $template; protected $expires; + protected $cache_control; protected $visible; protected $published; protected $publish_date; @@ -429,6 +430,9 @@ public function header($var = null) if (isset($this->header->expires)) { $this->expires = intval($this->header->expires); } + if (isset($this->header->cache_control)) { + $this->cache_control = $this->header->cache_control; + } if (isset($this->header->etag)) { $this->etag = (bool)$this->header->etag; } @@ -1250,6 +1254,22 @@ public function expires($var = null) return !isset($this->expires) ? Grav::instance()['config']->get('system.pages.expires') : $this->expires; } + /** + * Gets and sets the cache-control property. If not set it will return the default value (null) + * https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control for more details on valid options + * + * @param null $var + * @return null + */ + public function cacheControl($var = null) + { + if ($var !== null) { + $this->cache_control = $var; + } + + return !isset($this->cache_control) ? Grav::instance()['config']->get('system.pages.cache_control') : $this->cache_control; + } + /** * Gets and sets the title for this Page. If no title is set, it will use the slug() to get a name *