From aec344d30fcedadc930b8214f58feb96aec31cea Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Mon, 14 Feb 2022 14:15:39 +0530 Subject: [PATCH 1/8] Add page reloading in offline.php templates * Automatically reload the offline page once the user is back online. --- wp-includes/template.php | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/wp-includes/template.php b/wp-includes/template.php index fd3e859ac..a0c9c2a1e 100644 --- a/wp-includes/template.php +++ b/wp-includes/template.php @@ -171,3 +171,47 @@ function wp_service_worker_error_details_template( $output = '' ) { function wp_service_worker_error_message_placeholder() { echo '

'; } + +/** + * Reload the offline page and check if user comes online. + * + * @since 0.7 + */ +function wp_service_worker_offline_page_reload() { + if ( ! is_offline() ) { + return; + } + ?> + + Date: Tue, 15 Feb 2022 12:40:05 +0530 Subject: [PATCH 2/8] Add counter to delay reloading in each retry --- wp-includes/template.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/wp-includes/template.php b/wp-includes/template.php index a0c9c2a1e..22bf4ee63 100644 --- a/wp-includes/template.php +++ b/wp-includes/template.php @@ -182,7 +182,7 @@ function wp_service_worker_offline_page_reload() { return; } ?> - From 9f2e7c1bd0b5cdc59dcccff4b57b6784bb29dba2 Mon Sep 17 00:00:00 2001 From: Lovekesh Kumar Date: Tue, 15 Feb 2022 15:37:00 +0530 Subject: [PATCH 3/8] Update: Make fetch request to location.href instead of base origin Making fetch requests to the base origin i.e. fetch('.') will drop the query params. Co-authored-by: Weston Ruter --- wp-includes/template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-includes/template.php b/wp-includes/template.php index 22bf4ee63..2ea6b08f9 100644 --- a/wp-includes/template.php +++ b/wp-includes/template.php @@ -200,7 +200,7 @@ function wp_service_worker_offline_page_reload() { */ async function checkNetworkAndReload() { try { - const response = await fetch('.', {method: 'HEAD'}); + const response = await fetch(location.href, {method: 'HEAD'}); // Verify we get a valid response from the server if (response.status >= 200 && response.status < 500) { window.location.reload(); From 3ad9e561da5020a2f6d3aedb66f046045d01978d Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Tue, 15 Feb 2022 21:01:29 +0530 Subject: [PATCH 4/8] Add reloading script in offline and 500 templates and allow only on GET method --- wp-includes/template.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/wp-includes/template.php b/wp-includes/template.php index 2ea6b08f9..917ffbdf1 100644 --- a/wp-includes/template.php +++ b/wp-includes/template.php @@ -178,9 +178,14 @@ function wp_service_worker_error_message_placeholder() { * @since 0.7 */ function wp_service_worker_offline_page_reload() { - if ( ! is_offline() ) { + if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'GET' !== $_SERVER['REQUEST_METHOD'] ) { return; } + + if ( ! is_offline() && ! is_500() ) { + return; + } + ?> Date: Wed, 16 Feb 2022 20:50:59 +0530 Subject: [PATCH 6/8] Add test for wp_service_worker_offline_page_reload --- tests/test-template.php | 62 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/test-template.php diff --git a/tests/test-template.php b/tests/test-template.php new file mode 100644 index 000000000..2d3ed52d7 --- /dev/null +++ b/tests/test-template.php @@ -0,0 +1,62 @@ +assertEquals( 10, has_action( 'wp_footer', 'wp_service_worker_offline_page_reload' ) ); + $this->assertEquals( 10, has_action( 'error_footer', 'wp_service_worker_offline_page_reload' ) ); + + // Check when method is GET but not offline or 500. + $actual_script = wp_service_worker_offline_page_reload(); + $this->assertEquals( $_SERVER['REQUEST_METHOD'], 'GET' ); + $this->assertFalse( is_offline() ); + $this->assertFalse( is_500() ); + $this->assertEmpty( $actual_script ); + + // Check if script is added when offline. + $error_template_url = add_query_arg( 'wp_error_template', 'offline', home_url( '/', 'relative' ) ); + $this->go_to( $error_template_url ); + + $actual_script = wp_service_worker_offline_page_reload(); + $this->assertEquals( $_SERVER['REQUEST_METHOD'], 'GET' ); + $this->assertTrue( is_offline() ); + $this->assertFalse( is_500() ); + $this->assertStringContainsString( ' Date: Wed, 16 Feb 2022 22:55:21 +0530 Subject: [PATCH 8/8] Update test cases by adding output control function in output --- tests/test-template.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/test-template.php b/tests/test-template.php index 2d3ed52d7..6d81766f0 100644 --- a/tests/test-template.php +++ b/tests/test-template.php @@ -31,7 +31,9 @@ public function test_wp_service_worker_offline_page_reload() { $error_template_url = add_query_arg( 'wp_error_template', 'offline', home_url( '/', 'relative' ) ); $this->go_to( $error_template_url ); - $actual_script = wp_service_worker_offline_page_reload(); + ob_start(); + wp_service_worker_offline_page_reload(); + $actual_script = ob_get_clean(); $this->assertEquals( $_SERVER['REQUEST_METHOD'], 'GET' ); $this->assertTrue( is_offline() ); $this->assertFalse( is_500() ); @@ -42,7 +44,9 @@ public function test_wp_service_worker_offline_page_reload() { $error_template_url = add_query_arg( 'wp_error_template', '500', home_url( '/', 'relative' ) ); $this->go_to( $error_template_url ); - $actual_script = wp_service_worker_offline_page_reload(); + ob_start(); + wp_service_worker_offline_page_reload(); + $actual_script = ob_get_clean(); $this->assertEquals( $_SERVER['REQUEST_METHOD'], 'GET' ); $this->assertFalse( is_offline() ); $this->assertTrue( is_500() );