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

Guard against scripts/styles being printed in SW at wp_enqueue_scripts action #293

Closed
westonruter opened this issue Jun 9, 2020 · 0 comments · Fixed by #336
Closed

Guard against scripts/styles being printed in SW at wp_enqueue_scripts action #293

westonruter opened this issue Jun 9, 2020 · 0 comments · Fixed by #336

Comments

@westonruter
Copy link
Collaborator

The service worker fires wp_enqueue_scripts action:

if ( ! is_admin() ) {
wp_enqueue_scripts();
/**
* Fires before serving the frontend service worker, when its scripts should be registered, caching routes established, and assets precached.
*
* @since 0.2
*
* @param WP_Service_Worker_Scripts $scripts Instance to register service worker behavior with.
*/
do_action( 'wp_front_service_worker', $this->scripts );
} else {
$hook_name = 'service-worker';
set_current_screen( $hook_name );
/** This action is documented in wp-admin/admin-header.php */
do_action( 'admin_enqueue_scripts', $hook_name );
/**
* Fires before serving the wp-admin service worker, when its scripts should be registered, caching routes established, and assets precached.
*
* @since 0.2
*
* @param WP_Service_Worker_Scripts $scripts Instance to register service worker behavior with.
*/
do_action( 'wp_admin_service_worker', $this->scripts );
}

As discovered in https://wordpress.org/support/topic/sevice-worker-error/ some sites may erroneously print scripts or styles at wp_enqueue_scripts when they should rather be using the wp_print_styles or wp_print_scripts actions (or even wp_head).

To guard against this, the above logic should be wrapped in output buffering to suppress any output and then clean the buffer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant