diff --git a/inc/Engine/Common/PerformanceHints/Frontend/Processor.php b/inc/Engine/Common/PerformanceHints/Frontend/Processor.php index 7988bdc3bf..b48704c50e 100644 --- a/inc/Engine/Common/PerformanceHints/Frontend/Processor.php +++ b/inc/Engine/Common/PerformanceHints/Frontend/Processor.php @@ -191,8 +191,14 @@ private function inject_beacon( $html, $url, $is_mobile ): string { // Create the script tag. $script_tag = ""; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript $last_body_tag_position = strrpos( $html, '' ); - // Append the script tag just before the last closing body tag especially in cases where there's an iframe. - $html = substr_replace( $html, $inline_script . $script_tag . '', $last_body_tag_position, 7 ); + + if ( false !== $last_body_tag_position ) { + // Append the script tag just before the last closing body tag especially in cases where there's an iframe. + $html = substr_replace( $html, $inline_script . $script_tag . '', $last_body_tag_position, 7 ); + } else { + // Append to the end of html if is not found. + $html .= $inline_script . $script_tag; + } return $html; } diff --git a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/no_closing_body_tag_input.html b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/no_closing_body_tag_input.html new file mode 100644 index 0000000000..ae1390a63d --- /dev/null +++ b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/no_closing_body_tag_input.html @@ -0,0 +1,6 @@ + + + Test + + + diff --git a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/no_closing_body_tag_output.html b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/no_closing_body_tag_output.html new file mode 100644 index 0000000000..ac76359cdb --- /dev/null +++ b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/no_closing_body_tag_output.html @@ -0,0 +1,7 @@ + + + Test + + + + diff --git a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php index 4b661cd212..05c71f65b5 100644 --- a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php +++ b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php @@ -1,6 +1,8 @@ $html_with_double_body_output, ], + 'shouldAddBeaconWithoutClosingBodyTag' => [ + 'config' => [ + 'html' => $html_input_without_closing_body_tag, + 'atf' => [ + 'row' => null, + ], + 'lrc' => [ + 'row' => null, + ], + ], + 'expected' => $html_input_without_closing_body_tag_output, + ], ], ]; diff --git a/tests/Integration/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php b/tests/Integration/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php index 71a21a4cb3..882b5bb8ff 100644 --- a/tests/Integration/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php +++ b/tests/Integration/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php @@ -91,8 +91,8 @@ public function testShouldReturnAsExpected( $config, $expected ) { add_filter( 'pre_get_rocket_option_cache_logged_user', [ $this, 'get_cache_user' ] ); $this->assertSame( - $expected, - apply_filters( 'rocket_buffer', $config['html'] ) + trim($expected), + trim(apply_filters( 'rocket_buffer', $config['html'] )) ); }