19
19
class ReactInjectPlugin extends Renderer
20
20
{
21
21
22
+ // Allowed optimisations for
22
23
public $ actionFilter = [
23
24
'catalog_category_view ' ,
24
25
'cms_index_index ' ,
@@ -59,18 +60,20 @@ public function __construct(
59
60
*/
60
61
protected function renderAssetHtml (\Magento \Framework \View \Asset \PropertyGroup $ group )
61
62
{
63
+ @header ("x-built-with: Ract-Luma " , false );
62
64
$ startTime = microtime (true );
63
65
64
66
$ objectManager = \Magento \Framework \App \ObjectManager::getInstance ();
65
67
66
68
$ reactEnabled = boolval ($ this ->config ->getValue ('react_vue_config/react/enable ' ));
67
69
$ vueEnabled = boolval ($ this ->config ->getValue ('react_vue_config/vue/enable ' ));
68
- /* remove default magento Junky JS */
70
+ /* remove default Magento Junky JS */
69
71
$ removeAdobeJSJunk = boolval ($ this ->config ->getValue ('react_vue_config/junk/remove ' ));
70
72
$ removeCSSjunk = boolval ($ this ->config ->getValue ('react_vue_config/css/remove ' ));
73
+ $ criticalCSSHTML = boolval ($ this ->config ->getValue ('react_vue_config/css/critical ' ));
71
74
72
- if (isset ($ _GET ['css-test ' ])){
73
- // Css test mode
75
+ if (isset ($ _GET ['css-junk ' ])) {
76
+ // Css test mode
74
77
$ removeCSSjunk = false ;
75
78
}
76
79
@@ -93,6 +96,10 @@ protected function renderAssetHtml(\Magento\Framework\View\Asset\PropertyGroup $
93
96
$ assetOptimizedLarge = false ;
94
97
$ assetProductOptimized = false ;
95
98
$ assetCategoryOptimized = false ;
99
+ $ assetNotOptimisedMobile = false ;
100
+ $ optimisedProductCSSFileCriticalPath = false ;
101
+ $ optimisedCategoryCSSFileCriticalPath = false ;
102
+
96
103
$ removeController = in_array ($ actionName , $ this ->actionFilter );
97
104
$ isProduct = in_array ($ actionName , ['catalog_product_view ' ]);
98
105
$ isCategory = in_array ($ actionName , ['catalog_category_view ' , 'catalogsearch_result_index ' ]);
@@ -105,12 +112,15 @@ protected function renderAssetHtml(\Magento\Framework\View\Asset\PropertyGroup $
105
112
foreach ($ assets as $ key => $ asset ) {
106
113
if (in_array ($ actionName , $ this ->actionFilter ) && strpos ($ asset ->getUrl (), 'styles-m ' )) {
107
114
// http://**/static/version1642788857/frontend/Magento/luma/en_US/css/styles-m.css
115
+ $ assetNotOptimisedMobile = $ asset ->getUrl ();
108
116
$ optimisedCSSFileUrl = $ baseURL . 'styles-m.css ' ;
109
117
$ optimisedCSSFilePath = BP . '/pub/static/styles-m.css ' ;
118
+
110
119
$ optimisedProductCSSFileUrl = $ baseURL . 'product-styles-m.css ' ;
111
120
$ optimisedProductCSSFileCriticalUrl = $ baseURL . 'product-critical-m.css ' ;
112
121
$ optimisedProductCSSFileCriticalPath = BP . '/pub/static/product-critical-m.css ' ;
113
122
$ optimisedProductCSSFilePath = BP . '/pub/static/product-styles-m.css ' ;
123
+
114
124
$ optimisedCategoryCSSFileUrl = $ baseURL . 'category-styles-m.css ' ;
115
125
$ optimisedCategoryCSSFilePath = BP . '/pub/static/category-styles-m.css ' ;
116
126
$ optimisedCategoryCSSFileCriticalUrl = $ baseURL . 'category-critical-m.css ' ;
@@ -137,6 +147,7 @@ protected function renderAssetHtml(\Magento\Framework\View\Asset\PropertyGroup $
137
147
// http://**/static/version1642788857/frontend/Magento/luma/en_US/css/styles-l.css
138
148
$ optimisedCSSFileUrlLarge = $ baseURL . 'styles-l.css ' ;
139
149
$ optimisedCSSFilePathLarge = BP . '/pub/static/styles-l.css ' ;
150
+ $ assetNotOptimisedLarge = $ asset ->getUrl ();
140
151
if (file_exists ($ optimisedCSSFilePathLarge )) {
141
152
// echo $optimisedCSSFileUrl;
142
153
$ assetOptimizedLarge = $ optimisedCSSFileUrlLarge ;
@@ -239,27 +250,40 @@ protected function renderAssetHtml(\Magento\Framework\View\Asset\PropertyGroup $
239
250
240
251
if ($ removeCSSjunk ) {
241
252
// mobile CSS
242
- if ($ assetOptimized && !($ isProduct || $ isCategory )) {
253
+ if ($ assetOptimized && !($ assetProductOptimized || $ assetCategoryOptimized )) {
243
254
$ result = '<link rel="stylesheet" type="text/css" media="all" href=" ' . $ assetOptimized . '" /> ' . "\n" . $ result ;
244
255
}
245
256
if ($ assetOptimizedLarge ) {
246
257
$ result = '<link rel="stylesheet" type="text/css" media="screen and (min-width: 768px)" href=" ' . $ assetOptimizedLarge . '" /> ' . "\n" . $ result ;
247
258
}
248
259
if ($ assetProductOptimized && $ isProduct ) {
249
- if (file_exists ($ optimisedProductCSSFileCriticalPath )) {
250
- $ result = '<link rel="stylesheet" type="text/css" media="all" href=" ' . $ optimisedProductCSSFileCriticalUrl . '" /> ' . "\n" . $ result ;
260
+ if ($ optimisedProductCSSFileCriticalPath && file_exists ($ optimisedProductCSSFileCriticalPath )) {
261
+ @header ("Link: < " . $ optimisedProductCSSFileCriticalUrl . ">; rel=preload; as=style " , false );
262
+ if (!$ criticalCSSHTML ) {
263
+ $ result = '<link rel="stylesheet" type="text/css" media="all" href=" ' . $ optimisedProductCSSFileCriticalUrl . '" /> ' . "\n" . $ result ;
264
+ }
251
265
$ result = '<link rel="stylesheet" media="print" onload="this.onload=null;this.media= \'all \';" href=" ' . $ assetProductOptimized . '" /> ' . "\n" . $ result ;
252
266
} else {
253
267
$ result = '<link rel="stylesheet" type="text/css" media="all" href=" ' . $ assetProductOptimized . '" /> ' . "\n" . $ result ;
254
268
}
269
+ } else if (!$ assetProductOptimized && $ isProduct ) {
270
+ if ($ optimisedProductCSSFileCriticalPath && file_exists ($ optimisedProductCSSFileCriticalPath )) {
271
+ $ result = '<link rel="stylesheet" type="text/css" media="all" href=" ' . $ optimisedProductCSSFileCriticalUrl . '" /> ' . "\n" . $ result ;
272
+ $ result = '<link rel="stylesheet" media="print" onload="this.onload=null;this.media= \'all \';" href=" ' . $ assetNotOptimisedMobile . '" /> ' . "\n" . $ result ;
273
+ }
255
274
}
256
275
if ($ assetCategoryOptimized && $ isCategory ) {
257
- if (file_exists ($ optimisedCategoryCSSFileCriticalPath )) {
276
+ if ($ optimisedCategoryCSSFileCriticalPath && file_exists ($ optimisedCategoryCSSFileCriticalPath )) {
258
277
$ result = '<link rel="stylesheet" type="text/css" media="all" href=" ' . $ optimisedCategoryCSSFileCriticalUrl . '" /> ' . "\n" . $ result ;
259
278
$ result = '<link rel="stylesheet" media="print" onload="this.onload=null;this.media= \'all \';" href=" ' . $ assetCategoryOptimized . '" /> ' . "\n" . $ result ;
260
279
} else {
261
280
$ result = '<link rel="stylesheet" type="text/css" media="all" href=" ' . $ assetCategoryOptimized . '" /> ' . "\n" . $ result ;
262
281
}
282
+ } else if (!$ assetCategoryOptimized && $ isCategory ) {
283
+ if ($ optimisedCategoryCSSFileCriticalPath && file_exists ($ optimisedCategoryCSSFileCriticalPath )) {
284
+ $ result = '<link rel="stylesheet" type="text/css" media="all" href=" ' . $ optimisedProductCSSFileCriticalUrl . '" /> ' . "\n" . $ result ;
285
+ $ result = '<link rel="stylesheet" media="print" onload="this.onload=null;this.media= \'all \';" href=" ' . $ $ assetNotOptimisedLarge . '" /> ' . "\n" . $ result ;
286
+ }
263
287
}
264
288
}
265
289
@@ -276,6 +300,7 @@ protected function renderAssetHtml(\Magento\Framework\View\Asset\PropertyGroup $
276
300
277
301
public function checkFile ($ file )
278
302
{
303
+ // TODO: add APCu or file cache optimisation
279
304
return file_exists ($ file );
280
305
}
281
306
0 commit comments