Skip to content

Commit d8e166e

Browse files
committed
Enhance CSS loading logic in ReactInjectPlugin to check for critical CSS file existence before applying styles. This update ensures optimized CSS is loaded correctly based on the presence of critical CSS for product, category, and home pages.
1 parent 807323d commit d8e166e

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

ReactInjectPlugin.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,13 +520,19 @@ private function addOptimizedCSSLinks(string $result, array $pageTypes): string
520520
private function addProductCSSLinks(string $result, array $pageTypes): string
521521
{
522522
if ($this->assetVariables['assetProductOptimized'] && $pageTypes['isProduct']) {
523-
if ($this->assetVariables['optimisedProductCSSFileCriticalPath'] && $this->checkFile($this->assetVariables['optimisedProductCSSFileCriticalPath'])) {
523+
// Check if critical CSS file exists
524+
$criticalCSSExists = $this->assetVariables['optimisedProductCSSFileCriticalPath']
525+
&& $this->checkFile($this->assetVariables['optimisedProductCSSFileCriticalPath']);
526+
527+
if ($criticalCSSExists) {
528+
// Critical CSS exists: load critical CSS + optimized CSS with print media trick
524529
if (!$this->configuration['criticalCSSHTML']) {
525530
@header('Link: <' . $this->assetVariables['optimisedProductCSSFileCriticalUrl'] . '>; rel=preload; as=style', false);
526531
$result = '<link rel="stylesheet" type="text/css" media="all" href="' . $this->assetVariables['optimisedProductCSSFileCriticalUrl'] . '" />' . "\n" . $result;
527532
}
528533
$result = '<link rel="stylesheet" media="print" onload="this.onload=null;this.media=\'all\';" href="' . $this->assetVariables['assetProductOptimized'] . '" />' . "\n" . $result;
529534
} else {
535+
// Critical CSS doesn't exist: load optimized CSS as regular stylesheet (no print media trick)
530536
$result = '<link rel="stylesheet" type="text/css" media="all" href="' . $this->assetVariables['assetProductOptimized'] . '" />' . "\n" . $result;
531537
}
532538
} elseif (!$this->assetVariables['assetProductOptimized'] && $pageTypes['isProduct']) {
@@ -547,10 +553,16 @@ private function addProductCSSLinks(string $result, array $pageTypes): string
547553
private function addCategoryCSSLinks(string $result, array $pageTypes): string
548554
{
549555
if ($this->assetVariables['assetCategoryOptimized'] && $pageTypes['isCategory']) {
550-
if ($this->assetVariables['optimisedCategoryCSSFileCriticalPath'] && $this->checkFile($this->assetVariables['optimisedCategoryCSSFileCriticalPath'])) {
556+
// Check if critical CSS file exists
557+
$criticalCSSExists = $this->assetVariables['optimisedCategoryCSSFileCriticalPath']
558+
&& $this->checkFile($this->assetVariables['optimisedCategoryCSSFileCriticalPath']);
559+
560+
if ($criticalCSSExists) {
561+
// Critical CSS exists: load critical CSS + optimized CSS with print media trick
551562
$result = '<link rel="stylesheet" type="text/css" media="all" href="' . $this->assetVariables['optimisedCategoryCSSFileCriticalUrl'] . '" />' . "\n" . $result;
552563
$result = '<link rel="stylesheet" media="print" onload="this.onload=null;this.media=\'all\';" href="' . $this->assetVariables['assetCategoryOptimized'] . '" />' . "\n" . $result;
553564
} else {
565+
// Critical CSS doesn't exist: load optimized CSS as regular stylesheet (no print media trick)
554566
$result = '<link rel="stylesheet" type="text/css" media="all" href="' . $this->assetVariables['assetCategoryOptimized'] . '" />' . "\n" . $result;
555567
}
556568
} elseif (!$this->assetVariables['assetCategoryOptimized'] && $pageTypes['isCategory']) {
@@ -571,13 +583,19 @@ private function addCategoryCSSLinks(string $result, array $pageTypes): string
571583
private function addHomePageCSSLinks(string $result, array $pageTypes): string
572584
{
573585
if ($this->assetVariables['assetHomeOptimized'] && $pageTypes['isHome']) {
574-
if ($this->assetVariables['optimisedHomeCSSFileCriticalPath'] && $this->checkFile($this->assetVariables['optimisedHomeCSSFileCriticalPath'])) {
586+
// Check if critical CSS file exists
587+
$criticalCSSExists = $this->assetVariables['optimisedHomeCSSFileCriticalPath']
588+
&& $this->checkFile($this->assetVariables['optimisedHomeCSSFileCriticalPath']);
589+
590+
if ($criticalCSSExists) {
591+
// Critical CSS exists: load critical CSS + optimized CSS with print media trick
575592
if (!$this->configuration['criticalCSSHTML']) {
576593
@header('Link: <' . $this->assetVariables['optimisedHomeCSSFileCriticalUrl'] . '>; rel=preload; as=style', false);
577594
$result = '<link rel="stylesheet" type="text/css" media="all" href="' . $this->assetVariables['optimisedHomeCSSFileCriticalUrl'] . '" />' . "\n" . $result;
578595
}
579596
$result = '<link rel="stylesheet" media="print" onload="this.onload=null;this.media=\'all\';" href="' . $this->assetVariables['assetHomeOptimized'] . '" />' . "\n" . $result;
580597
} else {
598+
// Critical CSS doesn't exist: load optimized CSS as regular stylesheet (no print media trick)
581599
$result = '<link rel="stylesheet" type="text/css" media="all" href="' . $this->assetVariables['assetHomeOptimized'] . '" />' . "\n" . $result;
582600
}
583601
} elseif (!$this->assetVariables['assetHomeOptimized'] && $pageTypes['isHome']) {

0 commit comments

Comments
 (0)