Skip to content

Commit

Permalink
8478-Rocket-JS-Add-Ability-to-skip-scrips-from-defere
Browse files Browse the repository at this point in the history
  • Loading branch information
ihorvansach committed Jun 9, 2023
1 parent fb7f39c commit b961f61
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Model/Controller/ResultPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ public function aroundRenderResult(
return $result;
}

$ignoredStrings = $this->scopeConfig->getValue(
'mfrocketjavascript/general/ignore_deferred_javascript_with',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
$ignoredStrings = explode("\n", str_replace("\r", "\n", $ignoredStrings));
foreach ($ignoredStrings as $key => $ignoredString) {
$ignoredString = trim($ignoredString);
if (!$ignoredString) {
unset($ignoredStrings[$key]);
} else {
$ignoredStrings[$key] = $ignoredString;
}
}

$html = $response->getBody();
$scripts = [];

Expand Down Expand Up @@ -107,6 +120,13 @@ public function aroundRenderResult(
continue;
}

foreach ($ignoredStrings as $ignoredString) {
if (false !== stripos($script, $ignoredString)) {
$start++;
continue 2;
}
}

$html = str_replace($script, '', $html);
$scripts[] = $script;
}
Expand Down
7 changes: 7 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
</depends>
<comment>Enter page patches each in a new line. "*" means any path, you can use it at the beginning or end.</comment>
</field>
<field id="ignore_deferred_javascript_with" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label" type="textarea" canRestore="1">
<label>Ignore Deferred JavaScript With</label>
<depends>
<field id="enable_deferred_javascript">1</field>
</depends>
<comment><![CDATA[Enter ignored strings, each in a new line. JavaScript that contains these strings will not be moved to the bottom of the page. Scripts that contain <strong>data-rocketjavascript="false"</strong> will automatically be ignored. Example &lt;script data-rocketjavascript="false"&gt;/* some script *&lt;/script&gt;]]></comment>
</field>
<field id="enable_js_bundling_optimization" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label" type="select" canRestore="1">
<label>Enable JavaScript Bundling Optimization</label>
<comment>Please note that this option only works with enabled JavaScript Bundling (Configuration > Advanced > Developer > JavaScript Settings > Enable JavaScript Bundling).</comment>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<disallowed_pages_for_deferred_js>checkout/*
onestepcheckout/*
</disallowed_pages_for_deferred_js>
<ignore_deferred_javascript_with>www.googletagmanager.com</ignore_deferred_javascript_with>
<enable_js_bundling_optimization>1</enable_js_bundling_optimization>
<included_in_bundling>jquery.min.js
mage/common.min.js
Expand Down

0 comments on commit b961f61

Please sign in to comment.