Skip to content

Commit

Permalink
Added cache-control property #1591
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Jul 26, 2017
1 parent 1bfbb71 commit 13341c3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
6 changes: 6 additions & 0 deletions system/blueprints/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions system/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion system/src/Grav/Common/Grav.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
20 changes: 20 additions & 0 deletions system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Page
protected $parent;
protected $template;
protected $expires;
protected $cache_control;
protected $visible;
protected $published;
protected $publish_date;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 13341c3

Please sign in to comment.