Merge pull request #3062 from tinect/cacheJS
cache JS- and CSS-File to reduce response-time
This commit is contained in:
		| @@ -3,7 +3,15 @@ require_once $_SERVER['DOCUMENT_ROOT'] . '/modals/footer.php'; | ||||
| logger(); | ||||
| ?> | ||||
| <div style="margin-bottom: 100px;"></div> | ||||
| <script type='text/javascript'><?=$js_minifier->minify();?></script> | ||||
| <script type='text/javascript'><?php | ||||
|     $JSPath = '/tmp/' . $js_minifier->getDataHash() . '.js'; | ||||
|     if(file_exists($JSPath)) { | ||||
|         echo file_get_contents($JSPath); | ||||
|     } else { | ||||
|         echo $js_minifier->minify($JSPath); | ||||
|         cleanupCSS($css_minifier->getDataHash()); | ||||
|     } | ||||
|     ?></script> | ||||
| <script> | ||||
| <?php | ||||
| $lang_footer = json_encode($lang['footer']); | ||||
|   | ||||
| @@ -1612,4 +1612,22 @@ function solr_status() { | ||||
|   } | ||||
|   return false; | ||||
| } | ||||
|  | ||||
| function cleanupJS($ignore = '', $folder = '/tmp/*.js') { | ||||
|     foreach (glob($folder) as $filename) { | ||||
|         if(strpos($filename, $ignore) !== false) { | ||||
|             continue; | ||||
|         } | ||||
|         unlink($filename); | ||||
|     } | ||||
| } | ||||
|  | ||||
| function cleanupCSS($ignore = '', $folder = '/tmp/*.css') { | ||||
|     foreach (glob($folder) as $filename) { | ||||
|         if(strpos($filename, $ignore) !== false) { | ||||
|             continue; | ||||
|         } | ||||
|         unlink($filename); | ||||
|     } | ||||
| } | ||||
| ?> | ||||
|   | ||||
| @@ -30,7 +30,15 @@ | ||||
|       $css_minifier->add('/web/css/site/index.css'); | ||||
|     } | ||||
|   ?> | ||||
|   <style><?=$css_minifier->minify();?></style> | ||||
|   <style><?php | ||||
|       $CSSPath = '/tmp/' . $css_minifier->getDataHash() . '.css'; | ||||
|       if(file_exists($CSSPath)) { | ||||
|           echo file_get_contents($CSSPath); | ||||
|       } else { | ||||
|           echo $css_minifier->minify($CSSPath); | ||||
|           cleanupCSS($css_minifier->getDataHash()); | ||||
|       } | ||||
|       ?></style> | ||||
|   <?php if (strtolower(trim($DEFAULT_THEME)) != "lumen"): ?> | ||||
|   <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.7/<?= strtolower(trim($DEFAULT_THEME)); ?>/bootstrap.min.css"> | ||||
|   <?php endif; ?> | ||||
|   | ||||
							
								
								
									
										18
									
								
								data/web/inc/lib/CSSminifierExtended.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								data/web/inc/lib/CSSminifierExtended.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| <?php | ||||
|  | ||||
| use MatthiasMullie\Minify\CSS; | ||||
|  | ||||
| class CSSminifierExtended extends CSS { | ||||
|  | ||||
|     public function getDataHash() { | ||||
|         return sha1(json_encode($this->accessProtected($this,'data'))); | ||||
|     } | ||||
|  | ||||
|     private function accessProtected($obj, $prop) { | ||||
|         $reflection = new ReflectionClass($obj); | ||||
|         $property = $reflection->getProperty($prop); | ||||
|         $property->setAccessible(true); | ||||
|         return $property->getValue($obj); | ||||
|     } | ||||
|  | ||||
| } | ||||
							
								
								
									
										18
									
								
								data/web/inc/lib/JSminifierExtended.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								data/web/inc/lib/JSminifierExtended.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| <?php | ||||
|  | ||||
| use MatthiasMullie\Minify\JS; | ||||
|  | ||||
| class JSminifierExtended extends JS { | ||||
|  | ||||
|     public function getDataHash() { | ||||
|         return sha1(json_encode($this->accessProtected($this,'data'))); | ||||
|     } | ||||
|  | ||||
|     private function accessProtected($obj, $prop) { | ||||
|         $reflection = new ReflectionClass($obj); | ||||
|         $property = $reflection->getProperty($prop); | ||||
|         $property->setAccessible(true); | ||||
|         return $property->getValue($obj); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -19,16 +19,20 @@ require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/vendor/autoload.php'; | ||||
| // Load Sieve | ||||
| require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/sieve/SieveParser.php'; | ||||
|  | ||||
| // minifierExtended | ||||
| require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/JSminifierExtended.php'; | ||||
| require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/CSSminifierExtended.php'; | ||||
|  | ||||
| // Minify JS | ||||
| use MatthiasMullie\Minify; | ||||
| $js_minifier = new Minify\JS(); | ||||
| $js_minifier = new JSminifierExtended(); | ||||
| $js_dir = array_diff(scandir('/web/js/build'), array('..', '.')); | ||||
| foreach ($js_dir as $js_file) { | ||||
|   $js_minifier->add('/web/js/build/' . $js_file); | ||||
| } | ||||
|  | ||||
| // Minify CSS | ||||
| $css_minifier = new Minify\CSS(); | ||||
| $css_minifier = new CSSminifierExtended(); | ||||
| $css_dir = array_diff(scandir('/web/css/build'), array('..', '.')); | ||||
| foreach ($css_dir as $css_file) { | ||||
|   $css_minifier->add('/web/css/build/' . $css_file); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user