-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathDeferCSS.php
More file actions
174 lines (145 loc) · 6.49 KB
/
Copy pathDeferCSS.php
File metadata and controls
174 lines (145 loc) · 6.49 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
namespace React\React;
use Magento\Framework\App\Config\ScopeConfigInterface as Config;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Event\ObserverInterface;
class DeferCSS implements ObserverInterface
{
/**
* @var RequestInterface
*/
private $request;
public function __construct(
protected Config $config,
RequestInterface $request
) {
$this->request = $request;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
// Check if defer CSS is enabled (config or GET parameter)
if (!$this->shouldDeferCSS()) {
return;
}
$response = $observer->getEvent()->getData('response');
if (!$response) {
return;
}
$html = $response->getBody();
if ($html == '') {
return;
}
$html = $this->deferCSS($html);
$response->setBody($html);
}
/**
* Check if CSS deferral should be applied
*
* @return bool
*/
private function shouldDeferCSS(): bool
{
// Check GET parameter first (use request object)
$getParam = $this->request->getParam('defer-css');
if ($getParam === "false") {
return false;
}
if ($getParam === "true") {
return true;
}
// Fall back to config (default to true if not set)
$configValue = $this->config->getValue('react_vue_config/css/defer_css');
return $configValue === null || $configValue === '' ? true : boolval($configValue);
}
private function deferCSS(string $html): string
{
// Match link tags with styles-l.css (handles both /> and > closing, with or without whitespace)
$stylesLPattern = '@<link[^>]*href=["\'][^"\']*styles-l[^"\']*\.css[^"\']*["\'][^>]*\s*/?>@i';
$scriptsToInsert = [];
if (preg_match_all($stylesLPattern, $html, $allMatches, PREG_OFFSET_CAPTURE)) {
// Process matches in reverse order to maintain correct offsets when replacing
$matches = array_reverse($allMatches[0]);
foreach ($matches as $match) {
$stylesLTag = $match[0];
// Check if stylesheet has desktop-only media query (safe to defer on mobile)
if (!$this->hasDesktopMediaQuery($stylesLTag)) {
continue;
}
$href = $this->extractHref($stylesLTag);
$media = $this->extractMedia($stylesLTag);
// Build preload link for desktop (in HTML source)
$preloadLink = '<link rel="preload" as="style" fetchpriority="high" href="' . htmlspecialchars($href, ENT_QUOTES) . '"' . ($media ? ' media="' . htmlspecialchars($media, ENT_QUOTES) . '"' : '') . '>';
// Build the deferred script
// Desktop: uses document.write() for blocking synchronous load before FCP
// Mobile: uses setTimeout with createElement for async delayed load
$deferredScript = '<script no-defer>
(function () {
var isDesktop = window.matchMedia("(min-width: 768px)").matches;
if (isDesktop) {
// Acts as if the <link> was in original HTML → BLOCKS before FCP
document.write(
\'<link rel="stylesheet" href="' . htmlspecialchars($href, ENT_QUOTES) . '"' . ($media ? ' media="' . htmlspecialchars($media, ENT_QUOTES) . '"' : '') . '>\'
);
} else {
// Mobile: delayed, non-blocking
setTimeout(function () {
var l = document.createElement("link");
l.rel = "stylesheet";
l.href = "' . htmlspecialchars($href, ENT_QUOTES) . '";
' . ($media ? 'l.media = "' . htmlspecialchars($media, ENT_QUOTES) . '";' : '') . '
document.head.appendChild(l);
}, 0);
}
})();
</script>';
// Remove original link tag
$html = str_replace($stylesLTag, '', $html);
// Collect preload link and script to insert after mobile styles
$scriptsToInsert[] = $preloadLink . $deferredScript;
}
}
// Insert scripts after mobile styles (styles-m.css, category-styles-m.min.css, etc.)
// but before desktop styles
if (!empty($scriptsToInsert) && preg_match('/<head[^>]*>/i', $html, $headMatch, PREG_OFFSET_CAPTURE)) {
$headPos = $headMatch[0][1] + strlen($headMatch[0][0]);
$headContent = substr($html, $headPos);
// Find the last mobile stylesheet (styles-m.css or category-styles-m.min.css, etc.)
// Pattern: link tags with styles-m or category-styles-m or product-styles-m, etc.
$mobileStylesPattern = '@<link[^>]*href=["\'][^"\']*(?:styles-m|category-styles-m|product-styles-m|home-styles-m)[^"\']*\.css[^"\']*["\'][^>]*\s*/?>@i';
$insertPosition = 0;
if (preg_match_all($mobileStylesPattern, $headContent, $mobileMatches, PREG_OFFSET_CAPTURE)) {
// Find the position after the last mobile stylesheet
$lastMobileMatch = end($mobileMatches[0]);
$insertPosition = $lastMobileMatch[1] + strlen($lastMobileMatch[0]);
}
// Insert scripts after mobile styles (or at beginning of head if no mobile styles found)
$beforeScripts = substr($headContent, 0, $insertPosition);
$afterScripts = substr($headContent, $insertPosition);
$html = substr($html, 0, $headPos) . $beforeScripts . implode('', $scriptsToInsert) . $afterScripts;
}
return $html;
}
private function hasDesktopMediaQuery(string $linkTag): bool
{
if (preg_match('/media=["\']([^"\']+)["\']/i', $linkTag, $matches)) {
$media = $matches[1];
// Check if media query contains min-width (desktop-only)
return preg_match('/min-width\s*:\s*(\d+)/i', $media) === 1;
}
return false;
}
private function extractMedia(string $linkTag): string
{
if (preg_match('/media=["\']([^"\']+)["\']/i', $linkTag, $matches)) {
return $matches[1];
}
return '';
}
private function extractHref(string $linkTag): string
{
if (preg_match('/href=["\']([^"\']+)["\']/i', $linkTag, $matches)) {
return $matches[1];
}
return '';
}
}