From 0ea0a6bad1b713659fd7bc421e4ed2d4f5337a23 Mon Sep 17 00:00:00 2001 From: Opeyemi Ibrahim Date: Mon, 2 Sep 2024 11:27:35 +0100 Subject: [PATCH 1/3] :feat: Modify logic return for LRC when empty :closes: #6922 --- .../LazyRenderContent/Database/Rows/LazyRenderContent.php | 4 ++-- .../Optimization/LazyRenderContent/Frontend/Controller.php | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/inc/Engine/Optimization/LazyRenderContent/Database/Rows/LazyRenderContent.php b/inc/Engine/Optimization/LazyRenderContent/Database/Rows/LazyRenderContent.php index 3331179c7d..65f9ec73e3 100644 --- a/inc/Engine/Optimization/LazyRenderContent/Database/Rows/LazyRenderContent.php +++ b/inc/Engine/Optimization/LazyRenderContent/Database/Rows/LazyRenderContent.php @@ -92,7 +92,7 @@ public function __construct( $item ) { /** * Checks if the object has a valid LRC (Lazy Render Content) value. * - * @return bool Returns true if the object's status is 'completed' and the Below the fold value is not empty or 'not found', false otherwise. + * @return bool Returns true if the object's status is 'completed' and the Below the fold value is not empty or '[]', false otherwise. */ public function has_lrc() { if ( 'completed' !== $this->status ) { @@ -103,7 +103,7 @@ public function has_lrc() { return false; } - if ( 'not found' === $this->below_the_fold ) { + if ( '[]' === $this->below_the_fold ) { return false; } diff --git a/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php b/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php index b1a473646e..599fd41a4a 100644 --- a/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php +++ b/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php @@ -42,14 +42,15 @@ public function __construct( Processor $processor, ContextInterface $context ) { * @return string */ public function optimize( string $html, $row ): string { + $html_without_hashes = $this->remove_hashes( $html ); if ( ! $row->has_lrc() ) { - return $html; + return $html_without_hashes; } $hashes = json_decode( $row->below_the_fold ); if ( null === $hashes || ! is_array( $hashes ) ) { - return $html; + return $html_without_hashes; } $result = preg_replace( '/data-rocket-location-hash="(?:' . implode( '|', $hashes ) . ')"/i', 'data-wpr-lazyrender="1"', $html, -1, $count ); @@ -59,7 +60,7 @@ public function optimize( string $html, $row ): string { || 0 === $count ) { - return $html; + return $html_without_hashes; } $html = $result; From c77e44f09891753341d9186a9534870295fa944d Mon Sep 17 00:00:00 2001 From: Opeyemi Ibrahim Date: Mon, 2 Sep 2024 11:51:11 +0100 Subject: [PATCH 2/3] Modidy test data --- .../LazyRenderContent/Frontend/Controller/optimize.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller/optimize.php b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller/optimize.php index c55eee95d8..a5f5d85d7b 100644 --- a/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller/optimize.php +++ b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller/optimize.php @@ -46,4 +46,12 @@ 'html' => $single_line_hashed, 'expected' => $single_line_expected, ], + 'testShouldReturnEarlyWhenDBHasEmptyArray' => [ + 'config' => [ + 'has_lrc' => true, + 'below_the_fold' => '[]', + ], + 'html' => '
hello here
', + 'expected' => '
hello here
', + ], ]; From 5c93aed1d4b278ee2da24b19b1fb490a2f506258 Mon Sep 17 00:00:00 2001 From: Opeyemi Ibrahim Date: Tue, 3 Sep 2024 12:01:07 +0100 Subject: [PATCH 3/3] Minor PR review --- .../LazyRenderContent/Frontend/Controller.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php b/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php index 599fd41a4a..79e0590f14 100644 --- a/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php +++ b/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php @@ -42,15 +42,14 @@ public function __construct( Processor $processor, ContextInterface $context ) { * @return string */ public function optimize( string $html, $row ): string { - $html_without_hashes = $this->remove_hashes( $html ); if ( ! $row->has_lrc() ) { - return $html_without_hashes; + return $this->remove_hashes( $html ); } $hashes = json_decode( $row->below_the_fold ); - if ( null === $hashes || ! is_array( $hashes ) ) { - return $html_without_hashes; + if ( ! is_array( $hashes ) ) { + return $this->remove_hashes( $html ); } $result = preg_replace( '/data-rocket-location-hash="(?:' . implode( '|', $hashes ) . ')"/i', 'data-wpr-lazyrender="1"', $html, -1, $count ); @@ -60,7 +59,7 @@ public function optimize( string $html, $row ): string { || 0 === $count ) { - return $html_without_hashes; + return $this->remove_hashes( $html ); } $html = $result;