From 329a275f102e3a06d504bcd2dd0ac82af5fdf9e8 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Wed, 23 Nov 2016 08:42:47 +1100 Subject: [PATCH] Prevent code eval on cached assets When the request tells the server that the asset is cached, there is no point firing up the combiner, computing the contents and affecting performance. This change should result in a nice boost in secondary page load times. Thanks to @zerkms for pointing this out Fixes #2474 --- modules/system/classes/CombineAssets.php | 35 ++++++++++++++++-------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/modules/system/classes/CombineAssets.php b/modules/system/classes/CombineAssets.php index 99930cde0..95853825c 100644 --- a/modules/system/classes/CombineAssets.php +++ b/modules/system/classes/CombineAssets.php @@ -189,23 +189,34 @@ class CombineAssets $this->localPath = $cacheInfo['path']; $this->storagePath = storage_path('cms/combiner/assets'); - $this->setHashOnCombinerFilters($cacheKey); - - $combiner = $this->prepareCombiner($cacheInfo['files']); - $contents = $combiner->dump(); - $mime = ($cacheInfo['extension'] == 'css') ? 'text/css' : 'application/javascript'; - - header_remove(); - $response = Response::make($contents); - $response->header('Content-Type', $mime); + /* + * Analyse cache information + */ + $lastModifiedTime = gmdate("D, d M Y H:i:s \G\M\T", array_get($cacheInfo, 'lastMod')); + $etag = array_get($cacheInfo, 'etag'); + $mime = (array_get($cacheInfo, 'extension') == 'css') + ? 'text/css' + : 'application/javascript'; /* * Set 304 Not Modified header, if necessary */ - $lastModifiedTime = gmdate("D, d M Y H:i:s \G\M\T", array_get($cacheInfo, 'lastMod')); + header_remove(); + $response = Response::make(); + $response->header('Content-Type', $mime); $response->setLastModified(new DateTime($lastModifiedTime)); - $response->setEtag(array_get($cacheInfo, 'etag')); - $response->isNotModified(App::make('request')); + $response->setEtag($etag); + $modified = !$response->isNotModified(App::make('request')); + + /* + * Request says response is cached, no code evaluation needed + */ + if ($modified) { + $this->setHashOnCombinerFilters($cacheKey); + $combiner = $this->prepareCombiner($cacheInfo['files']); + $contents = $combiner->dump(); + $response->setContent($contents); + } return $response; }