From 6e7116b8cc48d578028881ec3d737ce4f379cd51 Mon Sep 17 00:00:00 2001 From: Fedik Date: Sat, 29 Jun 2019 10:13:06 +0300 Subject: [PATCH 1/3] Defer all text/javascript files by default --- libraries/src/Document/Document.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/libraries/src/Document/Document.php b/libraries/src/Document/Document.php index 5afe52ebdb16b..576ea1dba617a 100644 --- a/libraries/src/Document/Document.php +++ b/libraries/src/Document/Document.php @@ -525,6 +525,12 @@ public function addScript($url, $options = array(), $attribs = array()) $attribs['type'] = 'text/javascript'; } + // Defer all text/javascript files by default + if (!isset($attribs['defer']) && $attribs['type'] === 'text/javascript') + { + $attribs['defer'] = true; + } + $this->_scripts[$url] = isset($this->_scripts[$url]) ? array_replace($this->_scripts[$url], $attribs) : $attribs; $this->_scripts[$url]['options'] = isset($this->_scripts[$url]['options']) ? array_replace($this->_scripts[$url]['options'], $options) : $options; @@ -535,13 +541,13 @@ public function addScript($url, $options = array(), $attribs = array()) * Adds a script to the page * * @param string $content Script - * @param string $type Scripting mime (defaults to 'text/javascript') + * @param string $type Scripting mime (defaults to 'module') * * @return Document instance of $this to allow chaining * * @since 1.7.0 */ - public function addScriptDeclaration($content, $type = 'text/javascript') + public function addScriptDeclaration($content, $type = 'module') { if (!isset($this->_script[strtolower($type)])) { From d10368de3ec1903ade6cc610824678e8d1c31b88 Mon Sep 17 00:00:00 2001 From: Fedik Date: Sat, 29 Jun 2019 10:50:20 +0300 Subject: [PATCH 2/3] Defer DebugBar --- plugins/system/debug/JavascriptRenderer.php | 124 ++++++++++++++++++++ plugins/system/debug/debug.php | 10 +- 2 files changed, 127 insertions(+), 7 deletions(-) create mode 100644 plugins/system/debug/JavascriptRenderer.php diff --git a/plugins/system/debug/JavascriptRenderer.php b/plugins/system/debug/JavascriptRenderer.php new file mode 100644 index 0000000000000..227081726be7e --- /dev/null +++ b/plugins/system/debug/JavascriptRenderer.php @@ -0,0 +1,124 @@ +setEnableJqueryNoConflict(false); + $this->disableVendor('jquery'); + $this->disableVendor('fontawesome'); + } + + /** + * Renders the html to include needed assets + * + * Only useful if Assetic is not used + * + * @return string + * + * @since __DEPLOY_VERSION__ + */ + public function renderHead() + { + list($cssFiles, $jsFiles, $inlineCss, $inlineJs, $inlineHead) = $this->getAssets(null, self::RELATIVE_URL); + $html = ''; + + foreach ($cssFiles as $file) + { + $html .= sprintf('' . "\n", $file); + } + + foreach ($inlineCss as $content) + { + $html .= sprintf('' . "\n", $content); + } + + foreach ($jsFiles as $file) + { + $html .= sprintf('' . "\n", $file); + } + + foreach ($inlineJs as $content) + { + $html .= sprintf('' . "\n", $content); + } + + foreach ($inlineHead as $content) + { + $html .= $content . "\n"; + } + + return $html; + } + + /** + * Returns the code needed to display the debug bar + * + * AJAX request should not render the initialization code. + * + * @param boolean $initialize Whether or not to render the debug bar initialization code + * @param boolean $renderStackedData Whether or not to render the stacked data + * + * @return string + * + * @since __DEPLOY_VERSION__ + */ + public function render($initialize = true, $renderStackedData = true) + { + $js = ''; + + if ($initialize) + { + $js = $this->getJsInitializationCode(); + } + + if ($renderStackedData && $this->debugBar->hasStackedData()) + { + foreach ($this->debugBar->getStackedData() as $id => $data) + { + $js .= $this->getAddDatasetCode($id, $data, '(stacked)'); + } + } + + $suffix = !$initialize ? '(ajax)' : null; + $js .= $this->getAddDatasetCode($this->debugBar->getCurrentRequestId(), $this->debugBar->getData(), $suffix); + + if ($this->useRequireJs) + { + return "\n"; + } + else + { + return "\n"; + } + } +} diff --git a/plugins/system/debug/debug.php b/plugins/system/debug/debug.php index 81612928c85bd..1df6b8f8cd643 100644 --- a/plugins/system/debug/debug.php +++ b/plugins/system/debug/debug.php @@ -32,6 +32,7 @@ use Joomla\Plugin\System\Debug\DataCollector\ProfileCollector; use Joomla\Plugin\System\Debug\DataCollector\QueryCollector; use Joomla\Plugin\System\Debug\DataCollector\SessionCollector; +use Joomla\Plugin\System\Debug\JavascriptRenderer; use Joomla\Plugin\System\Debug\Storage\FileStorage; /** @@ -201,7 +202,7 @@ public function onAfterDispatch() { // Use our own jQuery and fontawesome instead of the debug bar shipped version $assetManager = $this->app->getDocument()->getWebAssetManager(); - $assetManager->enableAsset('jquery-noconflict'); + $assetManager->enableAsset('jquery'); $assetManager->enableAsset('fontawesome-free'); HTMLHelper::_('stylesheet', 'plg_system_debug/debug.css', array('version' => 'auto', 'relative' => true)); @@ -283,16 +284,11 @@ public function onAfterRespond() $this->debugBar->addCollector(new LanguageErrorsCollector($this->params)); } - $debugBarRenderer = $this->debugBar->getJavascriptRenderer(); + $debugBarRenderer = new JavascriptRenderer($this->debugBar, Uri::root(true) . '/media/vendor/debugbar/'); $openHandlerUrl = Uri::base(true) . '/index.php?option=com_ajax&plugin=debug&group=system&format=raw&action=openhandler'; $openHandlerUrl .= '&' . Session::getFormToken() . '=1'; $debugBarRenderer->setOpenHandlerUrl($openHandlerUrl); - $debugBarRenderer->setBaseUrl(Uri::root(true) . '/media/vendor/debugbar/'); - - $debugBarRenderer->disableVendor('jquery'); - $debugBarRenderer->setEnableJqueryNoConflict(false); - $debugBarRenderer->disableVendor('fontawesome'); /** * @todo disable highlightjs from the DebugBar, import it through NPM From 2cafd3e323f1041c245e3c4dd0691156355a17bc Mon Sep 17 00:00:00 2001 From: Fedik Date: Sat, 29 Jun 2019 11:22:11 +0300 Subject: [PATCH 3/3] phpcs --- plugins/system/debug/JavascriptRenderer.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/system/debug/JavascriptRenderer.php b/plugins/system/debug/JavascriptRenderer.php index 227081726be7e..3d21cdfa7bf2b 100644 --- a/plugins/system/debug/JavascriptRenderer.php +++ b/plugins/system/debug/JavascriptRenderer.php @@ -22,9 +22,9 @@ class JavascriptRenderer extends DebugBarJavascriptRenderer /** * Class constructor. * - * @param \DebugBar\DebugBar $debugBar - * @param string $baseUrl - * @param string $basePath + * @param \DebugBar\DebugBar $debugBar DebugBar instance + * @param string $baseUrl The base URL from which assets will be served + * @param string $basePath The path which assets are relative to * * @since __DEPLOY_VERSION__ */ @@ -85,8 +85,8 @@ public function renderHead() * * AJAX request should not render the initialization code. * - * @param boolean $initialize Whether or not to render the debug bar initialization code - * @param boolean $renderStackedData Whether or not to render the stacked data + * @param boolean $initialize Whether or not to render the debug bar initialization code + * @param boolean $renderStackedData Whether or not to render the stacked data * * @return string *