Skip to content

Commit

Permalink
Added optimize text/x-magento-init scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
hryvinskyi committed May 14, 2020
1 parent 6fe372a commit 62d3c14
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 14 deletions.
19 changes: 19 additions & 0 deletions Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Config extends AbstractHelper
const XML_HRYVINSKYI_DEFER_JS_GENERAL_ENABLED = 'hryvinskyi_defer_js/general/enabled';
const XML_HRYVINSKYI_DEFER_JS_DISABLE_ATTRIBUTE = 'hryvinskyi_defer_js/general/disable_attribute';
const XML_HRYVINSKYI_DEFER_JS_MINIFY_BODY_SCRIPTS = 'hryvinskyi_defer_js/general/minify_body_scripts';
const XML_HRYVINSKYI_DEFER_JS_OPTIMIZE_X_MAGENTO_INIT_SCRIPTS
= 'hryvinskyi_defer_js/general/optimize_x_magento_init_scripts';
const XML_HRYVINSKYI_DEFER_JS_EXCLUDE_CONTROLLERS = 'hryvinskyi_defer_js/general/exclude_controllers';
const XML_HRYVINSKYI_DEFER_JS_EXCLUDE_PATHS = 'hryvinskyi_defer_js/general/exclude_paths';
const XML_HRYVINSKYI_DEFER_JS_EXCLUDE_URL_PATTERN = 'hryvinskyi_defer_js/general/exclude_url_pattern';
Expand Down Expand Up @@ -78,6 +80,23 @@ public function isMinifyBodyScript(
);
}

/**
* @param string $scopeType
* @param null|string $scopeCode
*
* @return bool
*/
public function isOptimizeXMagentoInitScripts(
string $scopeType = ScopeInterface::SCOPE_STORE,
$scopeCode = null
): bool {
return $this->scopeConfig->isSetFlag(
self::XML_HRYVINSKYI_DEFER_JS_OPTIMIZE_X_MAGENTO_INIT_SCRIPTS,
$scopeType,
$scopeCode
);
}

/**
* Return Excluded Controllers
*
Expand Down
28 changes: 26 additions & 2 deletions Model/MoveJsToFooter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Hryvinskyi\DeferJs\Model;

use Hryvinskyi\Base\Helper\ArrayHelper;
use Hryvinskyi\Base\Helper\Json;
use Hryvinskyi\DeferJs\Helper\Config;
use Hryvinskyi\DeferJs\Model\Minify\MinifyJsInterface;
use Hryvinskyi\DeferJs\Model\PassesValidator\ValidateSkipper;
Expand Down Expand Up @@ -61,6 +63,7 @@ public function execute(Http $http)
$scripts = [];
$html = $http->getBody();

$jsons = [];
$scriptStart = '<script';
$scriptEnd = '</script>';

Expand All @@ -76,6 +79,15 @@ public function execute(Http $http)
$len = $end + strlen($scriptEnd) - $start;
$script = substr($html, $start, $len);

if (
$this->config->isOptimizeXMagentoInitScripts() &&
strpos($script, 'text/x-magento-init') !== false
) {
$jsons[] = strip_tags($script);
$html = str_replace($script, '', $html);
continue;
}

if ($this->validateSkipper->execute($script, $http)) {
$start++;
continue;
Expand All @@ -91,12 +103,24 @@ public function execute(Http $http)
$scripts = $this->minifyJs->execute($scripts);
}

$merged = [];
foreach ($jsons as $json) {
$json = Json::decode($json);
$merged = ArrayHelper::merge($merged, $json);
}

if (count($merged) > 0) {
$merged = '<script type=text/x-magento-init>' . Json::encode($merged) . '</script>';
} else {
$merged = '';
}

$scripts = implode('', $scripts);

if ($endBody = stripos($html, '</body>')) {
$html = substr($html, 0, $endBody) . $scripts . substr($html, $endBody);
$html = substr($html, 0, $endBody) . $merged . $scripts . substr($html, $endBody);
} else {
$html .= $scripts;
$html .= $merged . $scripts;
}

$http->setBody($html);
Expand Down
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,29 @@ Stores -> Configuration -> Hryvinskyi Extensions -> Defer JavaScripts and expand

- Ability to skip javascripts with a special tag that can be set in the admin panel
- Built-in skipping move Google tag manager (If you use a third-party module and can not add a special tag)
- Exclude Magento controllers from defer parsing of JavaScript.
- Exclude Magento store paths from defer parsing of JavaScript.
- Exclude by controllers from defer parsing of JavaScript.

*Controller will be unaffected by defer js. Use like: [module]_[controller]_[action] (Example: cms_index_index)*

- Exclude by store paths from defer parsing of JavaScript.

*Paths will be unaffected by defer js. Use like: /women/tops-women/jackets-women.html*


- Exclude by url pattern from defer parsing of JavaScript.

*URL pattern can be a full action name or a request path. Wildcards are allowed. Like:*

```
*cell-phones*
*cell-phones/nokia-2610-phone.html
customer_account_*
/customer/account/*
*?mode=list
```
- Minification of moved javascripts
- Optimize text/x-magento-init scripts
- Ability to extend the module with your validator
- Increased rendering time improves the Google Page Speed score.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scriptua/magento2-base": "~2.0.0"
},
"type": "magento2-module",
"version": "1.1.4",
"version": "1.1.5",
"license": "GPL-3.0-or-later",
"autoload": {
"files": [
Expand Down
21 changes: 13 additions & 8 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,29 @@
<field id="*/*/enabled">1</field>
</depends>
</field>
<field id="exclude_controllers" showInDefault="1" showInWebsite="1" showInStore="1" sortOrder="40"
<field id="optimize_x_magento_init_scripts" showInDefault="1" showInWebsite="1" showInStore="1"
sortOrder="40" translate="label comment" type="select">
<label>Enabled optimize x-magento-init scripts</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<depends>
<field id="*/*/enabled">1</field>
</depends>
</field>
<field id="exclude_controllers" showInDefault="1" showInWebsite="1" showInStore="1" sortOrder="50"
translate="label comment">
<label>Exclude controllers</label>
<frontend_model>\Hryvinskyi\DeferJs\Block\Adminhtml\System\ExcludeControllers</frontend_model>
<backend_model>\Hryvinskyi\DeferJs\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
<comment>Controller will be unaffected by defer js. Use like: [module]_[controller]_[action]
(Example: cms_index_index)
</comment>
<comment>Controller will be unaffected by defer js. Use like: [module]_[controller]_[action] (Example: cms_index_index)</comment>
</field>
<field id="exclude_paths" showInDefault="1" showInWebsite="1" showInStore="1" sortOrder="50"
<field id="exclude_paths" showInDefault="1" showInWebsite="1" showInStore="1" sortOrder="60"
translate="label comment">
<label>Exclude Paths</label>
<frontend_model>\Hryvinskyi\DeferJs\Block\Adminhtml\System\ExcludePaths</frontend_model>
<backend_model>\Hryvinskyi\DeferJs\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
<comment>Paths will be unaffected by defer js. Use like: /women/tops-women/jackets-women.html
</comment>
<comment>Paths will be unaffected by defer js. Use like: /women/tops-women/jackets-women.html</comment>
</field>
<field id="exclude_url_pattern" showInDefault="1" showInWebsite="1" showInStore="1" sortOrder="60"
<field id="exclude_url_pattern" showInDefault="1" showInWebsite="1" showInStore="1" sortOrder="70"
translate="label comment">
<label>Exclude Url Pattern</label>
<frontend_model>\Hryvinskyi\DeferJs\Block\Adminhtml\System\ExcludeUrlPattern</frontend_model>
Expand Down
3 changes: 2 additions & 1 deletion etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<general>
<enabled>0</enabled>
<disable_attribute><![CDATA[data-deferjs="false"]]></disable_attribute>
<minify_body_scripts>0</minify_body_scripts>
<minify_body_scripts>1</minify_body_scripts>
<optimize_x_magento_init_scripts>1</optimize_x_magento_init_scripts>
<exclude_controllers><![CDATA[[]]]></exclude_controllers>
<exclude_paths><![CDATA[[]]]></exclude_paths>
<exclude_url_pattern><![CDATA[[]]]></exclude_url_pattern>
Expand Down

0 comments on commit 62d3c14

Please sign in to comment.