Skip to content

3753 disallowed pages for deferred #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 94 additions & 1 deletion Model/Controller/ResultPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,33 @@ class ResultPlugin
*/
protected $scopeConfig;

/**
* @var bool
*/
protected $allowedOnPage;

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;

/**
* @param \Magento\Framework\App\RequestInterface $request
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
*/
public function __construct(
\Magento\Framework\App\RequestInterface $request,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Store\Model\StoreManagerInterface $storeManager = null
) {
$this->request = $request;
$this->scopeConfig = $scopeConfig;

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$this->storeManager = $storeManager ?: $objectManager->get(
\Magento\Store\Model\StoreManagerInterface::class
);
}

/**
Expand All @@ -59,6 +76,10 @@ public function aroundRenderResult(
return $result;
}

if (!$this->isAllowedOnPage()) {
return $result;
}

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

Expand Down Expand Up @@ -134,4 +155,76 @@ private function isEnabled()

return $enabled;
}

/**
* @return bool
*/
private function isAllowedOnPage()
{
if (null !== $this->allowedOnPage) {
return $this->allowedOnPage;
}
$this->allowedOnPage = false;

$spPages = $this->scopeConfig->getValue(
'mfrocketjavascript/general/disallowed_pages_for_deferred_js',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
$spPages = explode("\n", str_replace("\r", "\n", $spPages));

foreach ($spPages as $key => $path) {
$spPages[$key] = trim($spPages[$key]);
if (empty($spPages[$key])) {
unset($spPages[$key]);
}
}
$baseUrl = trim($this->storeManager->getStore()->getBaseUrl(), '/');
$baseUrl = str_replace('/index.php', '', $baseUrl);

$currentUrl = $this->storeManager->getStore()->getCurrentUrl();
$currentUrl = explode('?', $currentUrl);
$currentUrl = trim($currentUrl[0], '/');
foreach (['index.php', '.php', '.html'] as $end) {
$el = mb_strlen($end);
$cl = mb_strlen($currentUrl);
if (mb_strrpos($currentUrl, $end) == $cl - $el) {
$currentUrl = mb_substr($currentUrl, 0, $cl - $el);
}
}
$currentUrl = str_replace('/index.php', '', $currentUrl);
$currentUrl = trim($currentUrl, '/');
foreach ($spPages as $key => $path) {
$path = trim($path, '/');

if (mb_strlen($path)) {
if ('*' == $path{0}) {
$subPath = trim($path, '*/');
if (mb_strlen($currentUrl) - mb_strlen($subPath) === mb_strrpos($currentUrl, $subPath)) {
$this->allowedOnPage = true;
break;
}
}

if ('*' == $path{mb_strlen($path) - 1}) {
if (0 === mb_strpos($currentUrl, $baseUrl . '/' . trim($path, '*/'))) {
$this->allowedOnPage = true;
break;
}
}
if ($currentUrl == $baseUrl . '/' . trim($path, '/')) {
$this->allowedOnPage = true;
break;
}
} else {
//homepage

if ($currentUrl == $baseUrl) {
$this->allowedOnPage = true;
break;
}
}
}

return $this->allowedOnPage = !$this->allowedOnPage;
}
}
7 changes: 7 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
<comment>If enabled all JavaScript on storefront will be moved to the end of the page.</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="disallowed_pages_for_deferred_js" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label" type="textarea">
<label>Disallowed Pages for Deferred JavaScript</label>
<depends>
<field id="enable_deferred_javascript">1</field>
</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="enable_js_bundling_optimization" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label" type="select">
<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
3 changes: 3 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<default>
<mfrocketjavascript>
<general>
<disallowed_pages_for_deferred_js>checkout/*
onestepcheckout/*
</disallowed_pages_for_deferred_js>
<included_in_bundling>jquery/jquery.mobile.custom.min.js
mage/dataPost.min.js
mage/bootstrap.min.js
Expand Down