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

Try adding wp.hooks #234

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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 @@ -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;
}
Expand Down
9 changes: 9 additions & 0 deletions wp-includes/js/service-worker-navigation-routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down