-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathDeferJS.php
executable file
·38 lines (32 loc) · 1.04 KB
/
DeferJS.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
namespace React\React;
use Magento\Framework\App\Config\ScopeConfigInterface as Config;
use Magento\Framework\Event\ObserverInterface;
class DeferJS implements ObserverInterface
{
public function __construct(
protected Config $config
) {
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$removeAdobeJSJunk = boolval($this->config->getValue('react_vue_config/junk/remove'));
if ($removeAdobeJSJunk) {
return;
}
$response = $observer->getEvent()->getData('response');
if (!$response) {
return;
}
$html = $response->getBody();
if ($html == '') {
return;
}
$conditionalJsPattern = '@(?:<script type="text/javascript"|<script)(.*)</script>@msU';
preg_match_all($conditionalJsPattern, $html, $_matches);
$jsHtml = implode('', $_matches[0]);
$html = preg_replace($conditionalJsPattern, '', $html);
$html .= $jsHtml;
$response->setBody($html);
}
}