1
0
mirror of https://github.com/e107inc/e107.git synced 2025-01-17 04:38:27 +01:00

Added an option for pre-loading of library assets. More info: https://web.dev/uses-rel-preload/

This commit is contained in:
Cameron 2020-12-02 13:17:23 -08:00
parent 594c6043db
commit c9bc789b79
5 changed files with 72 additions and 4 deletions

View File

@ -2069,6 +2069,8 @@ class e107
* @param string $action
* - 'detect': Tries to detect a library and its installed version.
* - 'load': Loads a library.
* - 'info' : Load library information array
* - 'preload' : Activate preloading of the library (when detected) using a <link rel='preload' ...> tag.
* @param string $library
* The name of the library to detect/load.
* @param string $variant
@ -2145,6 +2147,35 @@ class e107
case 'info':
return $libraryHandler->info($library);
break;
case 'preload':
$info = $libraryHandler->info($library);
if(empty($info['preload']))
{
return null;
}
$libraryPath = self::getParser()->replaceConstants($info['library_path'].'/'.$info['path'], 'abs').'/';
foreach($info['preload'] as $pre)
{
$linkArr = [
'rel' => 'preload',
'href' => $libraryPath.$pre['path']
];
$browserCache = !empty($pre['browsercache']) ? true : false;
unset($pre['path'],$pre['browsercache']);
$linkArr2 = array_merge($linkArr,$pre);
self::link($linkArr2, $browserCache);
}
break;
}
}
@ -2260,11 +2291,12 @@ class e107
/**
* Add a <link> tag to the head of the html document.
* @param array $attributes
* @param bool $browserCache set to true to add the cacheId to the href.
* @example e107::link(array('rel'=>"dns-prefetch", "href" => "http://example-domain.com/"));
*/
public static function link($attributes=array())
public static function link($attributes=array(), $browserCache = false)
{
self::getJs()->addLink($attributes);
self::getJs()->addLink($attributes, $browserCache);
}

View File

@ -773,10 +773,17 @@ class e_jsmanager
/**
* Add a <link> tag to the head.
* @param array $attributes key>value pairs
* @param bool $browserCache - set to true to add the cacheId to the href.
* @example addLink(array('rel'=>'prefetch', 'href'=>THEME.'images/browsers.png'));
*/
public function addLink($attributes=array())
public function addLink($attributes=array(), $browserCache=false)
{
if(!empty($attributes['href']) && $browserCache === true)
{
$attributes['href'] .= "?".$this->getCacheId();
}
if(!empty($attributes))
{
$this->_e_link[] = $attributes;
@ -790,7 +797,6 @@ class e_jsmanager
*/
public function renderLinks()
{
if(empty($this->_e_link))
{
return null;
@ -812,6 +818,12 @@ class e_jsmanager
$text .= "\n<link";
foreach($v as $key=>$val)
{
if($key === 'crossorigin' && $val === true)
{
$text .= " crossorigin";
continue;
}
if(!empty($val))
{
$text .= " ".$key."=\"".$val."\"";

View File

@ -713,6 +713,23 @@ class core_library
// Override library path.
'library_path' => '{e_WEB}lib/font-awesome',
'path' => '4.7.0',
// preload in header using <link> tag. for speed optimization.
'preload' => array(
0 => array(
'as' => 'style',
'type' => '',
'path' => 'css/font-awesome.min.css',
'crossorigin' => false,
'browsercache' => true, // add the generated ?xxxxxx to the path.
),
1 => array(
'as' => 'font',
'type' => 'font/woff2',
'path' => 'fonts/fontawesome-webfont.woff2?v=4.7.0',
'crossorigin' => false,
'browsercache' => false,
),
),
);

View File

@ -189,12 +189,17 @@ class e_theme
}
e107::library('load', $library['name']);
e107::library('preload', $library['name']);
continue;
}
if($library['scope'] === $scope)
{
e107::library('load', $library['name']);
e107::library('preload', $library['name']);
continue;
}
}

View File

@ -2,7 +2,9 @@
if(e_ADMIN_AREA !==true)
{
e107::css('social', 'css/fontello.css');
e107::link(array('rel'=>'preload', 'href'=> e_PLUGIN_ABS."social/font/fontello.woff2", 'as' => "font", 'type'=>"font/woff2"), true);
e107::css('social' ,'css/social.css');
$appID = false;