-
Notifications
You must be signed in to change notification settings - Fork 100
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
How to keep service worker from intercepting WP REST requests? #1106
Comments
This should be the filter you're looking for: pwa-wp/wp-includes/components/class-wp-service-worker-navigation-routing-component.php Lines 408 to 419 in b4e13c7
Using plugin code like this: add_filter(
'wp_service_worker_navigation_route_denylist_patterns',
static function ( $patterns ) {
$patterns[] = '^\/wp-json';
return $patterns;
}
); |
@westonruter Thanks. I gave that a try and the initial request was still intercepted by the service worker which returned a 200 even though the endpoint returned a 500... ![]() ![]() I added the filter in a plugin where we have all of our custom back-end code. Does that filter need to be wrapped in another action potentially? |
No, it doesn't matter where you add the filter. I see it showing up in your service worker: const denylist = ["^\\\/wp-json","^\\\/wp\\-admin($|\\?|\/)","^[^\\?]*?\\.php($|\\?)","\\?(.*?&)?wp_service_worker=","^[^\\?]*?\\\/wp\\.serviceworker(\\?|$)","^[^\\?]*?\\\/feed\\\/(\\w+\\\/)?$","\\?(.*?&)?wp_customize=","\\?(.*?&)?customize_changeset_uuid=","^\\\/wp\\-json\\\/"].map(
(pattern) => new RegExp(pattern)
); Actually I even see pwa-wp/wp-includes/components/class-wp-service-worker-navigation-routing-component.php Lines 439 to 440 in 6134874
So no need to filter How can I reproduce the issue you're seeing on https://austin.showlists.dev/ ? |
@westonruter Yeah, I just looked at the code and noticed that line that was supposed to be excluding the REST api already as well. Hmmm. I can add you to our repo if you want to look at the code. I can't even recreate it locally because Chrome doesn't like the local SSL cert that was created by Local WP. So, the service worker doesn't load locally. I have to push to this staging site (.dev) in order to test anything PWA related which makes it kind of slow to diagnose. Right now I have the show submission endpoint on the |
@westonruter to answer your question on how to reproduce...
You should see that the initial call get's intercepted by the service worker which then returns a 200 instead of the 500 that it should be returning. |
Call it in what way? By navigating to it or by requesting it with |
@westonruter Sorry. Yes, requesting it with |
OK, here is a plugin with which I can reproduce the issue: <?php
/**
* Plugin Name: Try 500 REST API error
*/
add_action( 'rest_api_init', static function () {
register_rest_route(
'try',
'500-error',
array(
'methods' => array( 'GET', 'POST' ),
'callback' => static function () {
return new WP_REST_Response( 'I am an error', 500 );
},
'permission_callback' => '__return_true',
)
);
} );
add_action(
'wp_footer',
static function () {
?>
<script type="module">
const url = <?php echo wp_json_encode( rest_url( '/try/500-error/' ) ); ?>;
fetch( url, { method: 'POST' } ).then( ( response ) => {
console.info( response );
return response.json();
} ).then( ( json ) => {
console.info( json );
} ).catch( ( error ) => {
console.warn( error );
} );
</script>
<?php
}
); I am seeing a log entry coming from
The service worker is getting the 500 error response back as expected: But then the service worker is incorrectly responding with the HTML error page: Interestingly, if I change the request method from pwa-wp/wp-includes/js/service-worker-offline-post-request-handling.js Lines 147 to 151 in 56c64f1
Clearly this should be updated to pass through the body's response if the response's Here is some plugin code with a quick fix to disable this functionality: add_action(
'wp_front_service_worker',
static function ( WP_Service_Worker_Scripts $scripts ) {
unset( $scripts->registered['wp-offline-post-request-handling'] );
},
100
); |
@westonruter that quick fix works for now. Thanks for your help. |
We are using this plugin on showlists.net. It's a concert listing site. I recently ran into an issue where the service worker is intercepting WP REST requests and returning a 200 with what I assume is the 500 template. This was a custom WP REST endpoint of ours that a bug was introduced into. So the endpoint was actually returning a 500, but the user would have no idea that the request failed because the service worker is returning a 200 along with the 500 template HTML. That is having the effect of our error messaging not being shown because the request is getting back a 200 when it should be getting back a 500.
If I inspect the response from the service worker in Chrome, I see this in the Preview tab...
Is there a way to configure the service worker to ignore calls to any routes that start with
/wp-json
?The text was updated successfully, but these errors were encountered: