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 b1e0c2547..4a34cb1c9 100644 --- a/wp-includes/components/class-wp-service-worker-configuration-component.php +++ b/wp-includes/components/class-wp-service-worker-configuration-component.php @@ -120,8 +120,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; } 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; }