Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #6922 3.17: Hashes are not removed from the source when there is no LRC data #6937

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -103,7 +103,7 @@ public function has_lrc() {
return false;
}

if ( 'not found' === $this->below_the_fold ) {
if ( '[]' === $this->below_the_fold ) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public function __construct( Processor $processor, ContextInterface $context ) {
*/
public function optimize( string $html, $row ): string {
if ( ! $row->has_lrc() ) {
return $html;
return $this->remove_hashes( $html );
}

$hashes = json_decode( $row->below_the_fold );

if ( null === $hashes || ! is_array( $hashes ) ) {
return $html;
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 );
Expand All @@ -59,7 +59,7 @@ public function optimize( string $html, $row ): string {
||
0 === $count
) {
return $html;
return $this->remove_hashes( $html );
}

$html = $result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@
'html' => $single_line_hashed,
'expected' => $single_line_expected,
],
'testShouldReturnEarlyWhenDBHasEmptyArray' => [
'config' => [
'has_lrc' => true,
'below_the_fold' => '[]',
],
'html' => '<html><head></head><body><div data-rocket-location-hash="adc285f638b63c4110da1d803b711c40">hello here</div></body></html>',
'expected' => '<html><head></head><body><div >hello here</div></body></html>',
],
];
Loading