Skip to content

Commit b961f61

Browse files
committed
8478-Rocket-JS-Add-Ability-to-skip-scrips-from-defere
1 parent fb7f39c commit b961f61

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

Model/Controller/ResultPlugin.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ public function aroundRenderResult(
8080
return $result;
8181
}
8282

83+
$ignoredStrings = $this->scopeConfig->getValue(
84+
'mfrocketjavascript/general/ignore_deferred_javascript_with',
85+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
86+
$ignoredStrings = explode("\n", str_replace("\r", "\n", $ignoredStrings));
87+
foreach ($ignoredStrings as $key => $ignoredString) {
88+
$ignoredString = trim($ignoredString);
89+
if (!$ignoredString) {
90+
unset($ignoredStrings[$key]);
91+
} else {
92+
$ignoredStrings[$key] = $ignoredString;
93+
}
94+
}
95+
8396
$html = $response->getBody();
8497
$scripts = [];
8598

@@ -107,6 +120,13 @@ public function aroundRenderResult(
107120
continue;
108121
}
109122

123+
foreach ($ignoredStrings as $ignoredString) {
124+
if (false !== stripos($script, $ignoredString)) {
125+
$start++;
126+
continue 2;
127+
}
128+
}
129+
110130
$html = str_replace($script, '', $html);
111131
$scripts[] = $script;
112132
}

etc/adminhtml/system.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@
3939
</depends>
4040
<comment>Enter page patches each in a new line. "*" means any path, you can use it at the beginning or end.</comment>
4141
</field>
42+
<field id="ignore_deferred_javascript_with" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label" type="textarea" canRestore="1">
43+
<label>Ignore Deferred JavaScript With</label>
44+
<depends>
45+
<field id="enable_deferred_javascript">1</field>
46+
</depends>
47+
<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>
48+
</field>
4249
<field id="enable_js_bundling_optimization" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label" type="select" canRestore="1">
4350
<label>Enable JavaScript Bundling Optimization</label>
4451
<comment>Please note that this option only works with enabled JavaScript Bundling (Configuration > Advanced > Developer > JavaScript Settings > Enable JavaScript Bundling).</comment>

etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<disallowed_pages_for_deferred_js>checkout/*
1818
onestepcheckout/*
1919
</disallowed_pages_for_deferred_js>
20+
<ignore_deferred_javascript_with>www.googletagmanager.com</ignore_deferred_javascript_with>
2021
<enable_js_bundling_optimization>1</enable_js_bundling_optimization>
2122
<included_in_bundling>jquery.min.js
2223
mage/common.min.js

0 commit comments

Comments
 (0)