Skip to content

Commit 91bdb74

Browse files
committed
MC-16005: Deferred loading / parsing of JS p2
1 parent 645941c commit 91bdb74

File tree

4 files changed

+73
-19
lines changed

4 files changed

+73
-19
lines changed

app/code/Magento/Backend/etc/adminhtml/system.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@
182182
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
183183
<comment>Minification is not applied in developer mode.</comment>
184184
</field>
185+
<field id="move_inline_to_bottom" translate="label" type="select" sortOrder="25" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
186+
<label>Move JS code to the bottom of the page</label>
187+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
188+
</field>
185189
</group>
186190
<group id="css" translate="label" type="text" sortOrder="110" showInDefault="1" showInWebsite="1" showInStore="1">
187191
<label>CSS Settings</label>

app/code/Magento/Theme/Controller/Result/JsFooterPlugin.php

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,60 @@
55
*/
66
namespace Magento\Theme\Controller\Result;
77

8+
use Magento\Framework\App\Config\ScopeConfigInterface;
9+
use Magento\Store\Model\ScopeInterface;
10+
use Magento\Framework\App\Response\Http;
11+
812
/**
9-
* Plugin for putting messages to cookies
13+
* Plugin for putting all js to footer.
1014
*/
1115
class JsFooterPlugin
1216
{
17+
const XML_PATH_DEV_MOVE_JS_TO_BOTTOM = 'dev/js/move_inline_to_bottom';
18+
19+
/**
20+
* @var ScopeConfigInterface
21+
*/
22+
private $scopeConfig;
23+
24+
/**
25+
* @param ScopeConfigInterface $scopeConfig
26+
*/
27+
public function __construct(ScopeConfigInterface $scopeConfig)
28+
{
29+
$this->scopeConfig = $scopeConfig;
30+
}
31+
1332
/**
14-
* Put all javascript to footer before sending the response
33+
* Put all javascript to footer before sending the response.
1534
*
16-
* @param \Magento\Framework\App\Response\Http $subject
35+
* @param Http $subject
1736
* @return void
1837
*/
19-
public function beforeSendResponse(\Magento\Framework\App\Response\Http $subject)
38+
public function beforeSendResponse(Http $subject)
2039
{
21-
$content = $subject->getContent();
22-
$script = [];
23-
if (strpos($content, '</body') !== false) {
24-
$pattern = '#<script[^>]*+(?<!text/x-magento-template.)>.*?</script>#is';
25-
$content = preg_replace_callback(
26-
$pattern,
27-
function ($matchPart) use (&$script) {
28-
$script[] = $matchPart[0];
29-
return '';
30-
},
31-
$content
32-
);
33-
$subject->setContent(
34-
str_replace('</body', implode("\n", $script) . "\n</body", $content)
35-
);
40+
if (
41+
$this->scopeConfig->isSetFlag(
42+
self::XML_PATH_DEV_MOVE_JS_TO_BOTTOM,
43+
ScopeInterface::SCOPE_STORE
44+
)
45+
) {
46+
$content = $subject->getContent();
47+
$script = [];
48+
if (strpos($content, '</body') !== false) {
49+
$pattern = '#<script[^>]*+(?<!text/x-magento-template.)>.*?</script>#is';
50+
$content = preg_replace_callback(
51+
$pattern,
52+
function ($matchPart) use (&$script) {
53+
$script[] = $matchPart[0];
54+
return '';
55+
},
56+
$content
57+
);
58+
$subject->setContent(
59+
str_replace('</body', implode("\n", $script) . "\n</body", $content)
60+
);
61+
}
3662
}
3763
}
3864
}

app/code/Magento/Theme/etc/config.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ Disallow: /*SID=
6666
<static>
6767
<sign>1</sign>
6868
</static>
69+
<dev>
70+
<js>
71+
<move_inline_to_bottom>0</move_inline_to_bottom>
72+
</js>
73+
</dev>
6974
</dev>
7075
</default>
7176
</config>

app/code/Magento/Theme/etc/system.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
9+
<system>
10+
<section id="dev" translate="label" type="text" sortOrder="920" showInDefault="1" showInWebsite="1" showInStore="1">
11+
<group id="js" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
12+
<field id="move_inline_to_bottom" translate="label" type="select" sortOrder="25" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
13+
<label>Move JS code to the bottom of the page</label>
14+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
15+
</field>
16+
</group>
17+
</section>
18+
</system>
19+
</config>

0 commit comments

Comments
 (0)