1
0
mirror of https://github.com/e107inc/e107.git synced 2025-01-17 12:48:24 +01:00

Closes #618, #2599 Added simple options for including 'async' or 'defer' when using e107::js('header') and/or e107::js('footer').

Usage example: e107::js('footer', 'https://www.google.com/recaptcha/api.js?hl=en', ['defer','async']);
This commit is contained in:
Cameron 2021-12-27 15:21:18 -08:00
parent f58734346e
commit c96d64e275
2 changed files with 36 additions and 3 deletions

View File

@ -951,6 +951,10 @@ class e_jsmanager
// e107 Core Minimum should function independently of framework.
// ie. e107 Core Minimum: JS similar to e107 v1.0 should be loaded "e_js.php" (no framwork dependency)
// with basic functions like SyncWithServerTime() and expandit(), externalLinks() etc.
if(empty($opts))
{
$opts = [];
}
$pre = !empty($opts['pre']) ? $opts['pre'] : '';
$post = !empty($opts['post']) ? $opts['post'] : '';
@ -1105,11 +1109,16 @@ class e_jsmanager
$post
];
if(!empty($opts['defer']))
if(isset($opts['defer']) || in_array('defer', $opts, true))
{
$info[] = 'defer';
}
if(!empty($opts['async']) || in_array('async', $opts, true))
{
$info[] = 'async';
}
$file_path = implode($this->_sep, $info);
$zone = (int) $runtime_location;
@ -1134,11 +1143,16 @@ class e_jsmanager
$post
];
if(!empty($opts['defer']))
if(isset($opts['defer']) || in_array('defer', $opts, true))
{
$info[] = 'defer';
}
if(!empty($opts['async']) || in_array('async', $opts, true))
{
$info[] = 'async';
}
$file_path = implode($this->_sep, $info);
$zone = (int) $runtime_location;
@ -1531,7 +1545,7 @@ class e_jsmanager
if($pre) $pre .= "\n";
$post = varset($path[2], '');
if($post) $post = "\n".$post;
$inline = isset($path[3]) ? $path[3] : '';
$inline = isset($path[3]) ? str_replace($this->_sep, ' ',$path[3]) : '';
if($inline) $inline = " ".$inline;
$path = $path[0];

View File

@ -191,6 +191,11 @@
'zone' => 3,
'opts' => array('defer'=>true)
),
4 => array(
'file' => 'https://somewhere/async.js',
'zone' => 4,
'opts' => array('defer', 'async')
),
);
@ -210,6 +215,10 @@
$this->assertStringContainsString('<script type="text/javascript" src="https://somewhere/something.min.js" defer></script>', $result);
$this->assertStringContainsString('zone #3', $result);
$result = $this->js->renderJs('header', 4, true, true);
$this->assertStringContainsString('<script type="text/javascript" src="https://somewhere/async.js" defer async></script>', $result);
$this->assertStringContainsString('zone #4', $result);
}
public function testFooterFile()
@ -237,6 +246,12 @@
'opts' => array('defer'=>true)
),
4 => array(
'file' => 'https://somewhere/async.js',
'zone' => 4,
'opts' => array('defer', 'async')
),
);
@ -255,6 +270,10 @@
$this->assertStringContainsString('<script type="text/javascript" src="https://somewhere/something.min.js" defer></script>', $result);
$this->assertStringContainsString('priority #3', $result);
$result = $this->js->renderJs('footer', 4, true, true);
$this->assertStringContainsString('<script type="text/javascript" src="https://somewhere/async.js" defer async></script>', $result);
$this->assertStringContainsString('priority #4', $result);
}
/*
public function testSetDependency()