mirror of
https://github.com/e107inc/e107.git
synced 2025-03-14 17:39:46 +01:00
Experimental JS and CSS caching for improved performance. Currently disabled by default.
This commit is contained in:
parent
f938cc320f
commit
7cfcd1fd23
@ -314,6 +314,8 @@ foreach($CSSORDER as $val)
|
|||||||
|
|
||||||
unset($CSSORDER);
|
unset($CSSORDER);
|
||||||
|
|
||||||
|
e107::getJs()->renderCached('css');
|
||||||
|
|
||||||
if(deftrue('e_DEVELOPER'))
|
if(deftrue('e_DEVELOPER'))
|
||||||
{
|
{
|
||||||
echo "\n\n<!-- ======= [JSManager] FOOTER: Remaining JS ======= -->";
|
echo "\n\n<!-- ======= [JSManager] FOOTER: Remaining JS ======= -->";
|
||||||
@ -321,6 +323,8 @@ if(deftrue('e_DEVELOPER'))
|
|||||||
// [JSManager] Load JS Footer Includes by priority
|
// [JSManager] Load JS Footer Includes by priority
|
||||||
e107::getJs()->renderJs('footer', true);
|
e107::getJs()->renderJs('footer', true);
|
||||||
|
|
||||||
|
e107::getJs()->renderCached('js');
|
||||||
|
|
||||||
// [JSManager] Load JS Footer inline code by priority
|
// [JSManager] Load JS Footer inline code by priority
|
||||||
e107::getJs()->renderJs('footer_inline', true);
|
e107::getJs()->renderJs('footer_inline', true);
|
||||||
|
|
||||||
|
@ -313,8 +313,16 @@ else
|
|||||||
|
|
||||||
//TODO Additional options for 'bootstrap' and 'style' (ie. THEME_STYLE loaded above). Requires changes to js_manager.php
|
//TODO Additional options for 'bootstrap' and 'style' (ie. THEME_STYLE loaded above). Requires changes to js_manager.php
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$CSSORDER = deftrue('CSSORDER') ? explode(",",CSSORDER) : array('other','core','plugin','theme','inline');
|
$CSSORDER = deftrue('CSSORDER') ? explode(",",CSSORDER) : array('other','core','plugin','theme','inline');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
foreach($CSSORDER as $val)
|
foreach($CSSORDER as $val)
|
||||||
{
|
{
|
||||||
$cssId = $val."_css";
|
$cssId = $val."_css";
|
||||||
@ -323,6 +331,11 @@ foreach($CSSORDER as $val)
|
|||||||
|
|
||||||
unset($CSSORDER);
|
unset($CSSORDER);
|
||||||
|
|
||||||
|
|
||||||
|
$e_js->renderCached('css');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
$e_js->renderJs('other_css', false, 'css', false);
|
$e_js->renderJs('other_css', false, 'css', false);
|
||||||
echo "\n<!-- footer_other_css -->\n";
|
echo "\n<!-- footer_other_css -->\n";
|
||||||
@ -363,7 +376,7 @@ echo "\n<!-- footer_inline_css -->\n";
|
|||||||
e107::getJs()->renderJs('header', 1);
|
e107::getJs()->renderJs('header', 1);
|
||||||
e107::getJs()->renderJs('header_inline', 1);
|
e107::getJs()->renderJs('header_inline', 1);
|
||||||
|
|
||||||
// Send Javascript Libraries ALWAYS (for now)
|
// Send Javascript Libraries ALWAYS (for now) - loads e_jslib.php
|
||||||
$jslib = e107::getObject('e_jslib', null, e_HANDLER.'jslib_handler.php');
|
$jslib = e107::getObject('e_jslib', null, e_HANDLER.'jslib_handler.php');
|
||||||
$jslib->renderHeader('front', false);
|
$jslib->renderHeader('front', false);
|
||||||
|
|
||||||
@ -571,6 +584,10 @@ e107Event.trigger('loaded', null, document);
|
|||||||
|
|
||||||
e107::getJs()->renderJs('header_inline', 5);
|
e107::getJs()->renderJs('header_inline', 5);
|
||||||
|
|
||||||
|
|
||||||
|
e107::getJs()->renderCached('js');
|
||||||
|
|
||||||
|
|
||||||
echo "</head>\n";
|
echo "</head>\n";
|
||||||
|
|
||||||
|
|
||||||
|
@ -55,7 +55,15 @@ class e_jsmanager
|
|||||||
)
|
)
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamic List of files to be cached.
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $_cache_list = array();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
protected $_core_prefs = array();
|
protected $_core_prefs = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1125,6 +1133,8 @@ class e_jsmanager
|
|||||||
{
|
{
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$tp = e107::getParser();
|
$tp = e107::getParser();
|
||||||
echo "\n";
|
echo "\n";
|
||||||
if($label && E107_DEBUG_LEVEL > 0)
|
if($label && E107_DEBUG_LEVEL > 0)
|
||||||
@ -1145,10 +1155,14 @@ class e_jsmanager
|
|||||||
$pre = varset($path[2]) ? $path[2]."\n" : '';
|
$pre = varset($path[2]) ? $path[2]."\n" : '';
|
||||||
$post = varset($path[3]) ? "\n".$path[3] : '';
|
$post = varset($path[3]) ? "\n".$path[3] : '';
|
||||||
$path = $path[1];
|
$path = $path[1];
|
||||||
if(strpos($path, 'http') !== 0) $path = $tp->replaceConstants($path, 'abs').'?external=1&'.$this->getCacheId();
|
if(strpos($path, 'http') !== 0)
|
||||||
|
{
|
||||||
|
$path = $tp->replaceConstants($path, 'abs').'?external=1&'.$this->getCacheId();
|
||||||
|
}
|
||||||
|
|
||||||
echo $pre.'<link rel="stylesheet" media="'.$media.'" type="text/css" href="'.$path.'" />'.$post;
|
echo $pre.'<link rel="stylesheet" media="'.$media.'" type="text/css" href="'.$path.'" />'.$post;
|
||||||
echo "\n";
|
echo "\n";
|
||||||
|
// $this->cacheList['css'][] = $path;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
elseif($external) //true or 'js'
|
elseif($external) //true or 'js'
|
||||||
@ -1191,10 +1205,18 @@ class e_jsmanager
|
|||||||
$pre = varset($path[2]) ? $path[2]."\n" : '';
|
$pre = varset($path[2]) ? $path[2]."\n" : '';
|
||||||
$post = varset($path[3]) ? "\n".$path[3] : '';
|
$post = varset($path[3]) ? "\n".$path[3] : '';
|
||||||
$path = $path[1];
|
$path = $path[1];
|
||||||
if(strpos($path, 'http') !== 0) $path = $tp->replaceConstants($path, 'abs').'?'.$this->getCacheId();
|
if(strpos($path, 'http') !== 0) // local file.
|
||||||
|
{
|
||||||
|
if($this->addCache($external,$path) === true) // if cache enabled, then skip and continue.
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$path = $tp->replaceConstants($path, 'abs').'?'.$this->getCacheId();
|
||||||
|
}
|
||||||
|
|
||||||
echo $pre.'<link rel="stylesheet" media="'.$media.'" property="stylesheet" type="text/css" href="'.$path.'" />'.$post;
|
echo $pre.'<link rel="stylesheet" media="'.$media.'" property="stylesheet" type="text/css" href="'.$path.'" />'.$post;
|
||||||
echo "\n";
|
echo "\n";
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1207,6 +1229,12 @@ class e_jsmanager
|
|||||||
if($inline) $inline = " ".$inline;
|
if($inline) $inline = " ".$inline;
|
||||||
$path = $path[0];
|
$path = $path[0];
|
||||||
|
|
||||||
|
if(!$isExternal && $this->addCache('js',$path)===true)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if($external)
|
if($external)
|
||||||
{
|
{
|
||||||
// Never use CacheID on a CDN script, always render if it's CDN
|
// Never use CacheID on a CDN script, always render if it's CDN
|
||||||
@ -1215,15 +1243,24 @@ class e_jsmanager
|
|||||||
// don't render non CDN libs as external script calls when script consolidation is enabled
|
// don't render non CDN libs as external script calls when script consolidation is enabled
|
||||||
if($mod === 'core' || $mod === 'plugin' || $mod === 'theme')
|
if($mod === 'core' || $mod === 'plugin' || $mod === 'theme')
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
if(!e107::getPref('e_jslib_nocombine')) continue;
|
if(!e107::getPref('e_jslib_nocombine')) continue;
|
||||||
}
|
}
|
||||||
$path = $tp->replaceConstants($path, 'abs').'?'.$this->getCacheId();
|
$path = $tp->replaceConstants($path, 'abs').'?'.$this->getCacheId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
echo $pre.'<script type="text/javascript" src="'.$path.'"'.$inline.'></script>'.$post;
|
echo $pre.'<script type="text/javascript" src="'.$path.'"'.$inline.'></script>'.$post;
|
||||||
echo "\n";
|
echo "\n";
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// never try to consolidate external scripts
|
// never try to consolidate external scripts
|
||||||
if($isExternal) continue;
|
if($isExternal) continue;
|
||||||
$path = $tp->replaceConstants($path, '');
|
$path = $tp->replaceConstants($path, '');
|
||||||
@ -1233,9 +1270,100 @@ class e_jsmanager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return $lmodified;
|
return $lmodified;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $type string css|js
|
||||||
|
* @param $path
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function addCache($type,$path)
|
||||||
|
{
|
||||||
|
return false; //return false if cache is disabled - CURRENTLY DISABLED - TODO Add Pref etc.
|
||||||
|
|
||||||
|
$localPath = e107::getParser()->replaceConstants($path);
|
||||||
|
$this->_cache_list[$type][] = $localPath;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render Cached JS or CSS file.
|
||||||
|
* @param $type
|
||||||
|
*/
|
||||||
|
public function renderCached($type)
|
||||||
|
{
|
||||||
|
|
||||||
|
if(!empty($this->_cache_list[$type]))
|
||||||
|
{
|
||||||
|
$content = '';
|
||||||
|
$cacheId = $this->getCacheFileId($this->_cache_list[$type]);
|
||||||
|
|
||||||
|
$fileName = $cacheId.".".$type;
|
||||||
|
$saveFilePath = e_WEB.'cache/'.$fileName;
|
||||||
|
|
||||||
|
if(!is_readable($saveFilePath))
|
||||||
|
{
|
||||||
|
|
||||||
|
foreach($this->_cache_list[$type] as $k=>$path)
|
||||||
|
{
|
||||||
|
$content .= "\n\n/* ".str_replace("../",'',$path)." */ \n\n";
|
||||||
|
$content .= file_get_contents($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!@file_put_contents($saveFilePath, $content))
|
||||||
|
{
|
||||||
|
e107::getMessage()->addDebug("Couldn't save js/css cache file: ".$saveFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "\n\n<!-- Cached ".$type." -->\n";
|
||||||
|
|
||||||
|
if($type == 'js')
|
||||||
|
{
|
||||||
|
echo "<script type='text/javascript' src='".e_WEB_ABS."cache/".$fileName."'></script>\n\n";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
echo "<link type='text/css' href='".e_WEB_ABS."cache/".$fileName."' rel='stylesheet' property='stylesheet' />\n\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove from list, anything we have added.
|
||||||
|
foreach($this->_cache_list[$type] as $k=>$path)
|
||||||
|
{
|
||||||
|
unset($this->_cache_list[$type][$k]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function getCacheFileId($paths)
|
||||||
|
{
|
||||||
|
$id = '';
|
||||||
|
foreach($paths as $p)
|
||||||
|
{
|
||||||
|
$id .= str_replace("../","",$p);
|
||||||
|
}
|
||||||
|
|
||||||
|
return hash('crc32', $id) ;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render JS/CSS source array
|
* Render JS/CSS source array
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user