mirror of
https://github.com/getgrav/grav.git
synced 2026-03-05 12:01:37 +01:00
Added cache-control property #1591
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -227,15 +227,24 @@ class Grav extends Container
|
||||
|
||||
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';
|
||||
|
||||
@@ -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 @@ class Page
|
||||
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 @@ class Page
|
||||
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
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user