From 73357d4b86bff3489aba0ef78d97fb965d77d5b6 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 28 Jan 2020 09:39:15 -0800 Subject: [PATCH 1/2] Add wp.hooks library to service worker --- .../class-wp-service-worker-configuration-component.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/wp-includes/components/class-wp-service-worker-configuration-component.php b/wp-includes/components/class-wp-service-worker-configuration-component.php index 9ea9984f6..11640b51c 100644 --- a/wp-includes/components/class-wp-service-worker-configuration-component.php +++ b/wp-includes/components/class-wp-service-worker-configuration-component.php @@ -114,8 +114,11 @@ public function get_script() { } } + // Include wp.hooks for extensions to modify behavior of service worker with JS. + $script .= "\n// Include wp-includes/js/dist/hooks.js.\n" . file_get_contents( ABSPATH . WPINC . '/js/dist/hooks.js' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents + // Note: This includes the aliasing of `workbox` to `wp.serviceWorker`. - $script .= file_get_contents( PWA_PLUGIN_DIR . '/wp-includes/js/service-worker.js' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents + $script .= "\n// Include wp-includes/js/js/service-worker.js.\n" . file_get_contents( PWA_PLUGIN_DIR . '/wp-includes/js/service-worker.js' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents return $script; } From cad54c31297a556ad0af533bd551d1dd0e4a6c11 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 28 Jan 2020 10:25:35 -0800 Subject: [PATCH 2/2] Add navigation_response_success action when navigation responds successfully --- wp-includes/js/service-worker-navigation-routing.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/wp-includes/js/service-worker-navigation-routing.js b/wp-includes/js/service-worker-navigation-routing.js index 6874a74d3..1f44ac4d4 100644 --- a/wp-includes/js/service-worker-navigation-routing.js +++ b/wp-includes/js/service-worker-navigation-routing.js @@ -36,6 +36,15 @@ ERROR_OFFLINE_URL, ERROR_500_URL, NAVIGATION_DENYLIST_PATTERNS, ERROR_MESSAGES * async function handleNavigationRequest( { event } ) { const handleResponse = ( response ) => { if ( response.status < 500 ) { + /** + * Fires when service worker detected a request resulted in a successful response. + * + * @param {object} args + * @param {Event} args.event + * @param {Response} args.response + */ + wp.hooks.doAction( 'navigation_response_success', { event, response, navigationCacheStrategy } ); + return response; }