diff options
author | Michael Letzgus <www@chronos.michael-letzgus.de> | 2017-03-08 09:43:51 +0100 |
---|---|---|
committer | Michael Letzgus <www@chronos.michael-letzgus.de> | 2017-05-20 13:44:04 +0200 |
commit | fb9f13d4c13d2ea3ba70095f36e73c8915fce47f (patch) | |
tree | f64c25ff3cd53b1f9fbb8bc2a91c43699e62a231 /lib | |
parent | 6e3a914f4affde68c5cafa8fc7703efa3c3deaa6 (diff) | |
download | nextcloud-server-fb9f13d4c13d2ea3ba70095f36e73c8915fce47f.tar.gz nextcloud-server-fb9f13d4c13d2ea3ba70095f36e73c8915fce47f.zip |
Make page loading faster by deferred script loading:
* Create generalized function for emmitting <script defer src=""> tags to templates
* Remove type attribute from inline_js
* Add defer attribute to external <script> tags
Signed-off-by: Michael Letzgus <michaelletzgus@users.noreply.github.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/legacy/template.php | 4 | ||||
-rw-r--r-- | lib/private/legacy/template/functions.php | 37 |
2 files changed, 41 insertions, 0 deletions
diff --git a/lib/private/legacy/template.php b/lib/private/legacy/template.php index b4c69327438..4f7c11d0b64 100644 --- a/lib/private/legacy/template.php +++ b/lib/private/legacy/template.php @@ -12,6 +12,7 @@ * @author Joas Schilling <coding@schilljs.com> * @author Jörn Friedrich Dreyer <jfd@butonic.de> * @author Lukas Reschke <lukas@statuscode.ch> + * @author Michael Letzgus <develope@michael-letzgus.de> * @author Morris Jobke <hey@morrisjobke.de> * @author Raghu Nayyar <hey@raghunayyar.com> * @author Robin Appelman <robin@icewind.nl> @@ -208,6 +209,9 @@ class OC_Template extends \OC\Template\Base { $headers = ''; foreach(OC_Util::$headers as $header) { $headers .= '<'.\OCP\Util::sanitizeHTML($header['tag']); + if ( strcasecmp($header['tag'], 'script') === 0 && in_array('src', array_map('strtolower', array_keys($header['attributes']))) ) { + $headers .= ' defer'; + } foreach($header['attributes'] as $name=>$value) { $headers .= ' '.\OCP\Util::sanitizeHTML($name).'="'.\OCP\Util::sanitizeHTML($value).'"'; } diff --git a/lib/private/legacy/template/functions.php b/lib/private/legacy/template/functions.php index 7814918b815..06eb512b54f 100644 --- a/lib/private/legacy/template/functions.php +++ b/lib/private/legacy/template/functions.php @@ -7,6 +7,7 @@ * @author Joas Schilling <coding@schilljs.com> * @author Jörn Friedrich Dreyer <jfd@butonic.de> * @author Lukas Reschke <lukas@statuscode.ch> + * @author Michael Letzgus <develope@michael-letzgus.de> * @author Morris Jobke <hey@morrisjobke.de> * @author Robin McCorkell <robin@mccorkell.me.uk> * @author Roeland Jago Douma <roeland@famdouma.nl> @@ -38,6 +39,42 @@ function p($string) { } /** + * Prints a <script> tag with nonce and defer depending on config + * @param string $src the source URL, ignored when empty + * @param string $script_content the inline script content, ignored when empty + * @param bool $defer_flag deferred loading or not +*/ +function emit_script_tag($src, $script_content) { + $defer_str=' defer'; + $s='<script nonce="' . \OC::$server->getContentSecurityPolicyNonceManager()->getNonce() . '"'; + if (!empty($src)) { + // emit script tag for deferred loading from $src + $s.=$defer_str.' src="' . $src .'">'; + } else if (!empty($script_content)) { + // emit script tag for inline script from $script_content without defer (see MDN) + $s.=">\n".$script_content."\n"; + } else { + // no $src nor $src_content, really useless empty tag + $s.='>'; + } + $s.='</script>'; + print_unescaped($s."\n"); +} + +/** + * Print all <script> tags for loading JS + * @param hash $obj all the script information from template +*/ +function emit_script_loading_tags($obj) { + if (!empty($obj['inline_ocjs'])) { + emit_script_tag('', $obj['inline_ocjs']); + } + foreach($obj['jsfiles'] as $jsfile) { + emit_script_tag($jsfile, ''); + } +} + +/** * Prints an unsanitized string - usage of this function may result into XSS. * Consider using p() instead. * @param string|array $string the string which will be printed as it is |