Skip to content

Commit

Permalink
#304 - Divide the max_age in half, as it takes 2 request to refresh it
Browse files Browse the repository at this point in the history
  • Loading branch information
johanjanssens committed May 11, 2020
1 parent 535c0fd commit c6988bb
Showing 1 changed file with 38 additions and 29 deletions.
67 changes: 38 additions & 29 deletions code/site/components/com_pages/resources/scripts/proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,42 @@
$content = $data['content'];
$status = $data['status'];

$age = max(time() - strtotime($headers['Date']), 0);
$max_age = false;
$age = max(time() - strtotime($headers['Date']), 0);

//Check the cache is stale
if(isset($headers['Cache-Control']))
{
$cache_control = explode(',', $headers['Cache-Control']);

foreach ($cache_control as $key => $value)
{
if(is_string($value))
{
$parts = explode('=', $value);

if (count($parts) > 1)
{
unset( $cache_control[$key]);
$cache_control[trim($parts[0])] = trim($parts[1]);
}
}
}


if (isset($cache_control['max-age'])) {
$max_age = $cache_control['max-age'];
}

if (isset($cache_control['s-maxage'])) {
$max_age = $cache_control['s-maxage'];
}
}

//Divide the max_age in half and set it in the $_SERVER global
if($headers['Cache-Status'] != 'EXPIRED') {
$_SERVER['HTTP_CACHE_CONTROL'] = 'max-age='.(int) ($max_age / 2);
}

//Cache validation
if(isset($_SERVER['HTTP_IF_NONE_MATCH']) && isset($headers['Etag']))
Expand Down Expand Up @@ -139,36 +174,10 @@
header_remove('Age');

//Check the cache is stale
if(isset($headers['Cache-Control']))
if($max_age !== false && $age > $max_age)
{
$cache_control = explode(',', $headers['Cache-Control']);

foreach ($cache_control as $key => $value)
{
if(is_string($value))
{
$parts = explode('=', $value);

if (count($parts) > 1)
{
unset( $cache_control[$key]);
$cache_control[trim($parts[0])] = trim($parts[1]);
}
}
}

if (isset($cache_control['max-age'])) {
$max_age = $cache_control['max-age'];
}

if (isset($cache_control['s-maxage'])) {
$max_age = $cache_control['s-maxage'];
}

//Se: https://tools.ietf.org/html/rfc7234#section-5.5.1
if($age > $max_age) {
header('Warning: 110 Joomlatools/Pages "Response is Stale"');
}
header('Warning: 110 Joomlatools/Pages "Response is Stale"');
}

//Send the content
Expand Down

0 comments on commit c6988bb

Please sign in to comment.