-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4354 from magento-performance/MC-17449
[Performance] Introduce CSS critical path and font display swap
- Loading branch information
Showing
20 changed files
with
363 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\Theme\Block\Html\Header; | ||
|
||
use Magento\Framework\View\Element\Block\ArgumentInterface; | ||
use Magento\Framework\View\Asset\Repository; | ||
use Magento\Framework\View\Asset\File\NotFoundException; | ||
|
||
/** | ||
* This ViewModel will add inline critical css in case dev/css/use_css_critical_path is enabled. | ||
*/ | ||
class CriticalCss implements ArgumentInterface | ||
{ | ||
/** | ||
* @var Repository | ||
*/ | ||
private $assetRepo; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $filePath; | ||
|
||
/** | ||
* @param Repository $assetRepo | ||
* @param string $filePath | ||
*/ | ||
public function __construct( | ||
Repository $assetRepo, | ||
string $filePath = '' | ||
) { | ||
$this->assetRepo = $assetRepo; | ||
$this->filePath = $filePath; | ||
} | ||
|
||
/** | ||
* Returns critical css data as string. | ||
* | ||
* @return bool|string | ||
*/ | ||
public function getCriticalCssData() | ||
{ | ||
try { | ||
$asset = $this->assetRepo->createAsset($this->filePath, ['_secure' => 'false']); | ||
$content = $asset->getContent(); | ||
} catch (NotFoundException $e) { | ||
$content = ''; | ||
} | ||
|
||
return $content; | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
app/code/Magento/Theme/Controller/Result/AsyncCssPlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Theme\Controller\Result; | ||
|
||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Store\Model\ScopeInterface; | ||
use Magento\Framework\App\Response\Http; | ||
|
||
/** | ||
* Plugin for asynchronous CSS loading. | ||
*/ | ||
class AsyncCssPlugin | ||
{ | ||
const XML_PATH_USE_CSS_CRITICAL_PATH = 'dev/css/use_css_critical_path'; | ||
|
||
/** | ||
* @var ScopeConfigInterface | ||
*/ | ||
private $scopeConfig; | ||
|
||
/** | ||
* @param ScopeConfigInterface $scopeConfig | ||
*/ | ||
public function __construct(ScopeConfigInterface $scopeConfig) | ||
{ | ||
$this->scopeConfig = $scopeConfig; | ||
} | ||
|
||
/** | ||
* Load CSS asynchronously if it is enabled in configuration. | ||
* | ||
* @param Http $subject | ||
* @return void | ||
*/ | ||
public function beforeSendResponse(Http $subject) | ||
{ | ||
$content = $subject->getContent(); | ||
|
||
if (\is_string($content) && strpos($content, '</body') !== false && $this->scopeConfig->isSetFlag( | ||
self::XML_PATH_USE_CSS_CRITICAL_PATH, | ||
ScopeInterface::SCOPE_STORE | ||
)) { | ||
$cssMatches = []; | ||
// add link rel preload to style sheets | ||
$content = preg_replace_callback( | ||
'@<link\b.*?rel=("|\')stylesheet\1.*?/>@', | ||
function ($matches) use (&$cssMatches) { | ||
$cssMatches[] = $matches[0]; | ||
preg_match('@href=("|\')(.*?)\1@', $matches[0], $hrefAttribute); | ||
$href = $hrefAttribute[2]; | ||
if (preg_match('@media=("|\')(.*?)\1@', $matches[0], $mediaAttribute)) { | ||
$media = $mediaAttribute[2]; | ||
} | ||
$media = $media ?? 'all'; | ||
$loadCssAsync = sprintf( | ||
'<link rel="preload" as="style" media="%s" . | ||
onload="this.onload=null;this.rel=\'stylesheet\'"' . | ||
'href="%s">', | ||
$media, | ||
$href | ||
); | ||
|
||
return $loadCssAsync; | ||
}, | ||
$content | ||
); | ||
|
||
if (!empty($cssMatches)) { | ||
$content = str_replace('</body', implode("\n", $cssMatches) . "\n</body", $content); | ||
$subject->setContent($content); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> | ||
<system> | ||
<section id="dev"> | ||
<group id="css"> | ||
<field id="use_css_critical_path" translate="label comment" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1"> | ||
<label>Use CSS critical path</label> | ||
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model> | ||
<comment>CSS files are loading synchronously by default.</comment> | ||
</field> | ||
</group> | ||
</section> | ||
</system> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
app/code/Magento/Theme/view/frontend/templates/html/header/criticalCss.phtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
/** | ||
* @var \Magento\Theme\Block\Html\Header\CriticalCss $criticalCssViewModel | ||
*/ | ||
?> | ||
<?php $criticalCssViewModel = $block->getData('criticalCssViewModel'); ?> | ||
|
||
<style type="text/css" data-type="criticalCss"> | ||
<?= /* @noEscape */ $criticalCssViewModel->getCriticalCssData() ?> | ||
</style> |
13 changes: 13 additions & 0 deletions
13
app/code/Magento/Theme/view/frontend/templates/html/main_css_preloader.phtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
?> | ||
<div data-role="main-css-loader" class="loading-mask"> | ||
<div class="loader"> | ||
<img src="<?= $block->escapeUrl($block->getViewFileUrl('images/loader-1.gif')); ?>" | ||
alt="<?= $block->escapeHtml(__('Loading...')); ?>" | ||
style="position: absolute;"> | ||
</div> | ||
</div> |
10 changes: 10 additions & 0 deletions
10
app/code/Magento/Theme/view/frontend/templates/js/css_rel_preload.phtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
?> | ||
<script> | ||
/*! loadCSS rel=preload polyfill. [c]2017 Filament Group, Inc. MIT License */ | ||
!function(t){"use strict";t.loadCSS||(t.loadCSS=function(){});var e=loadCSS.relpreload={};if(e.support=function(){var e;try{e=t.document.createElement("link").relList.supports("preload")}catch(t){e=!1}return function(){return e}}(),e.bindMediaToggle=function(t){var e=t.media||"all";function a(){t.media=e}t.addEventListener?t.addEventListener("load",a):t.attachEvent&&t.attachEvent("onload",a),setTimeout(function(){t.rel="stylesheet",t.media="only x"}),setTimeout(a,3e3)},e.poly=function(){if(!e.support())for(var a=t.document.getElementsByTagName("link"),n=0;n<a.length;n++){var o=a[n];"preload"!==o.rel||"style"!==o.getAttribute("as")||o.getAttribute("data-loadcss")||(o.setAttribute("data-loadcss",!0),e.bindMediaToggle(o))}},!e.support()){e.poly();var a=t.setInterval(e.poly,500);t.addEventListener?t.addEventListener("load",function(){e.poly(),t.clearInterval(a)}):t.attachEvent&&t.attachEvent("onload",function(){e.poly(),t.clearInterval(a)})}"undefined"!=typeof exports?exports.loadCSS=loadCSS:t.loadCSS=loadCSS}("undefined"!=typeof global?global:this); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,4 +42,8 @@ | |
._block-content-loading { | ||
position: relative; | ||
} | ||
|
||
[data-role='main-css-loader'] { | ||
display: none; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.