Skip to content

Commit 6de67fd

Browse files
committed
Merge branch '2.3-develop' of github.com:magento/magento2ce into MC-15729
2 parents 5f0bf18 + 19c4a50 commit 6de67fd

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Theme\Controller\Result;
7+
8+
/**
9+
* Plugin for putting messages to cookies
10+
*/
11+
class JsFooterPlugin
12+
{
13+
/**
14+
* Put all javascript to footer before sending the response
15+
*
16+
* @param \Magento\Framework\App\Response\Http $subject
17+
* @return void
18+
*/
19+
public function beforeSendResponse(\Magento\Framework\App\Response\Http $subject)
20+
{
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+
);
36+
}
37+
}
38+
}

app/code/Magento/Theme/etc/frontend/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@
2626
<type name="Magento\Framework\Controller\ResultInterface">
2727
<plugin name="result-messages" type="Magento\Theme\Controller\Result\MessagePlugin"/>
2828
</type>
29+
<type name="Magento\Framework\App\Response\Http">
30+
<plugin name="result-js-footer" type="Magento\Theme\Controller\Result\JsFooterPlugin"/>
31+
</type>
2932
</config>

0 commit comments

Comments
 (0)