Improved asset caching, when enabled the server will send a 304 Not Modified header

This commit is contained in:
Samuel Georges 2014-12-17 13:46:08 +11:00
parent 8ae095b36e
commit 512e91118f
2 changed files with 27 additions and 6 deletions

View File

@ -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) * **Build 171** (2014-12-17)
- Add new methods `propertyName()` and `paramName()` to Component base class for accessing names of external properties. - Add new methods `propertyName()` and `paramName()` to Component base class for accessing names of external properties.

View File

@ -1,5 +1,6 @@
<?php namespace System\Classes; <?php namespace System\Classes;
use App;
use URL; use URL;
use File; use File;
use Lang; use Lang;
@ -14,6 +15,7 @@ use Assetic\Asset\GlobAsset;
use Assetic\Asset\AssetCache; use Assetic\Asset\AssetCache;
use Assetic\Cache\FilesystemCache; use Assetic\Cache\FilesystemCache;
use System\Classes\ApplicationException; use System\Classes\ApplicationException;
use DateTime;
/** /**
* Class used for combining JavaScript and StyleSheet * Class used for combining JavaScript and StyleSheet
@ -148,8 +150,23 @@ class CombineAssets
header_remove(); header_remove();
$response = Response::make($contents); $response = Response::make($contents);
$response->header('Content-Type', $mime); $response->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; return $response;
} }
@ -356,11 +373,12 @@ class CombineAssets
if (!$cacheInfo) { if (!$cacheInfo) {
$combiner = $this->prepareCombiner($assets); $combiner = $this->prepareCombiner($assets);
$version = $combiner->getLastModified(); $lastMod = $combiner->getLastModified();
$cacheInfo = [ $cacheInfo = [
'output' => $cacheId.'-'.$version, 'version' => $cacheId.'-'.$lastMod,
'version' => $version, 'etag' => $cacheId,
'lastMod' => $lastMod,
'files' => $assets, 'files' => $assets,
'path' => $this->path, 'path' => $this->path,
'extension' => $extension 'extension' => $extension
@ -369,7 +387,7 @@ class CombineAssets
$this->putCache($cacheId, $cacheInfo); $this->putCache($cacheId, $cacheInfo);
} }
return $this->getCombinedUrl($cacheInfo['output']); return $this->getCombinedUrl($cacheInfo['version']);
} }
/** /**