From 512e91118f3f4650bfcdf27c599412a83c8013c5 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Wed, 17 Dec 2014 13:46:08 +1100 Subject: [PATCH] Improved asset caching, when enabled the server will send a 304 Not Modified header --- CHANGELOG.md | 3 +++ modules/system/classes/CombineAssets.php | 30 +++++++++++++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89762f6b6..997c3606f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +* **Build 17x** (2014-12-xx) + - Improved asset caching (`cms.enableAssetCache`), when enabled the server will send a *304 Not Modified* header. + * **Build 171** (2014-12-17) - Add new methods `propertyName()` and `paramName()` to Component base class for accessing names of external properties. diff --git a/modules/system/classes/CombineAssets.php b/modules/system/classes/CombineAssets.php index 1c94fc784..23e952ef0 100644 --- a/modules/system/classes/CombineAssets.php +++ b/modules/system/classes/CombineAssets.php @@ -1,5 +1,6 @@ header('Content-Type', $mime); - $response->header('Cache-Control', 'max-age=31536000, public'); - $response->header('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + 2678400)); + + /* + * Set 304 Not Modified header, if necessary + */ + if ($this->useCache) { + $lastModifiedTime = gmdate("D, d M Y H:i:s \G\M\T", array_get($cacheInfo, 'lastMod')); + $response->setLastModified(new DateTime($lastModifiedTime)); + $response->setEtag(array_get($cacheInfo, 'etag')); + $response->isNotModified(App::make('request')); + } + /* + * The request has always expired + */ + else { + $response->header('Cache-Control', 'max-age=31536000, public'); + $response->header('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + 2678400)); + } return $response; } @@ -356,11 +373,12 @@ class CombineAssets if (!$cacheInfo) { $combiner = $this->prepareCombiner($assets); - $version = $combiner->getLastModified(); + $lastMod = $combiner->getLastModified(); $cacheInfo = [ - 'output' => $cacheId.'-'.$version, - 'version' => $version, + 'version' => $cacheId.'-'.$lastMod, + 'etag' => $cacheId, + 'lastMod' => $lastMod, 'files' => $assets, 'path' => $this->path, 'extension' => $extension @@ -369,7 +387,7 @@ class CombineAssets $this->putCache($cacheId, $cacheInfo); } - return $this->getCombinedUrl($cacheInfo['output']); + return $this->getCombinedUrl($cacheInfo['version']); } /**