Skip to content

Commit 3ed4110

Browse files
authored
Merge pull request #5 from yuriyhamulevych/3753-disallowed-pages-for-deferred
3753 disallowed pages for deferred
2 parents 40c387a + 912eea6 commit 3ed4110

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

Model/Controller/ResultPlugin.php

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,33 @@ class ResultPlugin
3030
*/
3131
protected $scopeConfig;
3232

33+
/**
34+
* @var bool
35+
*/
36+
protected $allowedOnPage;
37+
38+
/**
39+
* @var \Magento\Store\Model\StoreManagerInterface
40+
*/
41+
protected $storeManager;
42+
3343
/**
3444
* @param \Magento\Framework\App\RequestInterface $request
3545
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
46+
* @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
3647
*/
3748
public function __construct(
3849
\Magento\Framework\App\RequestInterface $request,
39-
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
50+
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
51+
\Magento\Store\Model\StoreManagerInterface $storeManager = null
4052
) {
4153
$this->request = $request;
4254
$this->scopeConfig = $scopeConfig;
55+
56+
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
57+
$this->storeManager = $storeManager ?: $objectManager->get(
58+
\Magento\Store\Model\StoreManagerInterface::class
59+
);
4360
}
4461

4562
/**
@@ -59,6 +76,10 @@ public function aroundRenderResult(
5976
return $result;
6077
}
6178

79+
if (!$this->isAllowedOnPage()) {
80+
return $result;
81+
}
82+
6283
$html = $response->getBody();
6384
$scripts = [];
6485

@@ -134,4 +155,76 @@ private function isEnabled()
134155

135156
return $enabled;
136157
}
158+
159+
/**
160+
* @return bool
161+
*/
162+
private function isAllowedOnPage()
163+
{
164+
if (null !== $this->allowedOnPage) {
165+
return $this->allowedOnPage;
166+
}
167+
$this->allowedOnPage = false;
168+
169+
$spPages = $this->scopeConfig->getValue(
170+
'mfrocketjavascript/general/disallowed_pages_for_deferred_js',
171+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
172+
);
173+
$spPages = explode("\n", str_replace("\r", "\n", $spPages));
174+
175+
foreach ($spPages as $key => $path) {
176+
$spPages[$key] = trim($spPages[$key]);
177+
if (empty($spPages[$key])) {
178+
unset($spPages[$key]);
179+
}
180+
}
181+
$baseUrl = trim($this->storeManager->getStore()->getBaseUrl(), '/');
182+
$baseUrl = str_replace('/index.php', '', $baseUrl);
183+
184+
$currentUrl = $this->storeManager->getStore()->getCurrentUrl();
185+
$currentUrl = explode('?', $currentUrl);
186+
$currentUrl = trim($currentUrl[0], '/');
187+
foreach (['index.php', '.php', '.html'] as $end) {
188+
$el = mb_strlen($end);
189+
$cl = mb_strlen($currentUrl);
190+
if (mb_strrpos($currentUrl, $end) == $cl - $el) {
191+
$currentUrl = mb_substr($currentUrl, 0, $cl - $el);
192+
}
193+
}
194+
$currentUrl = str_replace('/index.php', '', $currentUrl);
195+
$currentUrl = trim($currentUrl, '/');
196+
foreach ($spPages as $key => $path) {
197+
$path = trim($path, '/');
198+
199+
if (mb_strlen($path)) {
200+
if ('*' == $path{0}) {
201+
$subPath = trim($path, '*/');
202+
if (mb_strlen($currentUrl) - mb_strlen($subPath) === mb_strrpos($currentUrl, $subPath)) {
203+
$this->allowedOnPage = true;
204+
break;
205+
}
206+
}
207+
208+
if ('*' == $path{mb_strlen($path) - 1}) {
209+
if (0 === mb_strpos($currentUrl, $baseUrl . '/' . trim($path, '*/'))) {
210+
$this->allowedOnPage = true;
211+
break;
212+
}
213+
}
214+
if ($currentUrl == $baseUrl . '/' . trim($path, '/')) {
215+
$this->allowedOnPage = true;
216+
break;
217+
}
218+
} else {
219+
//homepage
220+
221+
if ($currentUrl == $baseUrl) {
222+
$this->allowedOnPage = true;
223+
break;
224+
}
225+
}
226+
}
227+
228+
return $this->allowedOnPage = !$this->allowedOnPage;
229+
}
137230
}

etc/adminhtml/system.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
<comment>If enabled all JavaScript on storefront will be moved to the end of the page.</comment>
3333
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
3434
</field>
35+
<field id="disallowed_pages_for_deferred_js" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label" type="textarea">
36+
<label>Disallowed Pages for Deferred JavaScript</label>
37+
<depends>
38+
<field id="enable_deferred_javascript">1</field>
39+
</depends>
40+
<comment>Enter page patches each in a new line. "*" means any path, you can use it at the beginning or end.</comment>
41+
</field>
3542
<field id="enable_js_bundling_optimization" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label" type="select">
3643
<label>Enable JavaScript Bundling Optimization</label>
3744
<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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
<default>
1212
<mfrocketjavascript>
1313
<general>
14+
<disallowed_pages_for_deferred_js>checkout/*
15+
onestepcheckout/*
16+
</disallowed_pages_for_deferred_js>
1417
<included_in_bundling>jquery/jquery.mobile.custom.min.js
1518
mage/dataPost.min.js
1619
mage/bootstrap.min.js

0 commit comments

Comments
 (0)