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
This commit is contained in:
Samuel Georges 2016-11-23 08:42:47 +11:00
parent 6bbf496ab2
commit 329a275f10
1 changed files with 23 additions and 12 deletions

View File

@ -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;
}