-
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
Introduce is_service_worker() template conditional #301
Comments
@jono-alderson What is the use case? Note that the XML Sitemaps did not include an |
The XML omission is an oversight, which we should revisit. URLs like As a practical example, pwa-wp/wp-includes/general-template.php Lines 90 to 101 in 77c0944
is_500 and is_offline , but there's no equivalent for is_pwa ; adding this check without a 'clean' function like this, for any/every type of PWA output, is going to become cumbersome.
Given that different consumers (search engines, social media platforms, etc) handle 'feeds' and similar in different ways, I'm anticipating a need to be able to filter an |
But in this example there is no need to check for whether it is serving a service worker. What is the use case you have for the service worker response specifically? |
Revisiting, as:
Example scenario; /**
* Checks if the request is for a webpage
*
* @return bool
*/
private function request_is_for_webpage() : bool {
// Service workers.
if ( get_query_var( 'wp_service_worker', false ) ) {
return false;
}
if ( function_exists( 'is_offline' ) && is_offline() ) {
return false;
}
if ( function_exists( 'is_500' ) && is_500() ) {
return false;
}
// WP non-page scenarios.
if ( is_favicon() || is_feed() || is_robots() || wp_is_json_request() || wp_is_jsonp_request() || wp_is_xml_request() ) {
return false;
}
// XML sitemaps.
if ( get_query_var( 'sitemap', false ) ) {
return false;
}
return true;
} Consistent methods for service worker related functionality (and XML sitemaps) would streamline this, and make it more maintainable. |
Right, this logic has evolved. It's now: pwa-wp/wp-includes/service-workers.php Lines 195 to 204 in 18c2932
If you flush rewrite rules then the query var should still be set due to: pwa-wp/wp-includes/class-wp.php Lines 16 to 27 in 7c63ceb
Humm, but you can see there that the rewrite rule does get flushed automatically once you're log in. At what point are you calling |
Works on |
TIL: |
Well, that's an interesting mess! Thanks for catching. I'll just use the 'wrong' hooks instead 😆 |
Update for WordPress 6.1: Moving the send_headers action to later in the load |
As raised by @jono-alderson in a support topic, it may be useful for there to be and
is_service_worker()
template conditional in the same way as we haveis_offline()
andis_500()
. Core also hasis_robots()
andis_favicon()
as “template” conditionals. I'm not sure how often these are used, however.For non-template conditionals, core also has
wp_is_json_request()
,wp_is_jsonp_request()
, andwp_is_xml_request()
.Implementation of
is_service_worker()
is as simple as:However, we may want to also include logic to make sure it returns true for the admin service worker as well (which is served by admin-ajax).
The text was updated successfully, but these errors were encountered: