From 4c3da17b868f97305ddaff8d36c6696979caf3cb Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sun, 25 Jul 2021 22:21:16 -0700 Subject: [PATCH] Utilize workbox-window to register SW; eliminate wordpress_sw_installed cookie --- wp-includes/service-workers.php | 53 ++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/wp-includes/service-workers.php b/wp-includes/service-workers.php index 61054f89d..48308852c 100644 --- a/wp-includes/service-workers.php +++ b/wp-includes/service-workers.php @@ -138,7 +138,6 @@ function wp_print_service_workers() { } global $pagenow; - $scopes = array(); $home_port = wp_parse_url( home_url(), PHP_URL_PORT ); $admin_port = wp_parse_url( admin_url(), PHP_URL_PORT ); @@ -152,40 +151,46 @@ function wp_print_service_workers() { $on_front_domain = isset( $_SERVER['HTTP_HOST'] ) && $home_url === $_SERVER['HTTP_HOST']; $on_admin_domain = isset( $_SERVER['HTTP_HOST'] ) && $admin_url === $_SERVER['HTTP_HOST']; - // Install the front service worker if currently on the home domain. - if ( $on_front_domain ) { - $scopes[ WP_Service_Workers::SCOPE_FRONT ] = home_url( '/', 'relative' ); // The home_url() here will account for subdirectory installs. - } + $sw_src = null; + $scope = null; // Include admin service worker if it seems it will be used (and it can be installed). - if ( $on_admin_domain && ( is_user_logged_in() || is_admin() || in_array( $pagenow, array( 'wp-login.php', 'wp-signup.php', 'wp-activate.php' ), true ) ) ) { - $scopes[ WP_Service_Workers::SCOPE_ADMIN ] = wp_parse_url( admin_url( '/' ), PHP_URL_PATH ); + if ( $on_admin_domain && ( is_admin() || in_array( $pagenow, array( 'wp-login.php', 'wp-signup.php', 'wp-activate.php' ), true ) ) ) { + $sw_src = wp_get_service_worker_url( WP_Service_Workers::SCOPE_ADMIN ); + $scope = wp_parse_url( admin_url( '/' ), PHP_URL_PATH ); + } elseif ( $on_front_domain ) { + $sw_src = wp_get_service_worker_url( WP_Service_Workers::SCOPE_FRONT ); + $scope = home_url( '/', 'relative' ); // The home_url() here will account for subdirectory installs. } - if ( empty( $scopes ) ) { + if ( ! $sw_src || ! $scope ) { return; } + $workbox_window_src = sprintf( '%s/wp-includes/js/workbox-v%s/workbox-window.%s.js', PWA_PLUGIN_URL, PWA_WORKBOX_VERSION, SCRIPT_DEBUG ? 'dev' : 'prod' ); + $register_options = array( + 'scope' => $scope, + ); + + // phpcs:disable Squiz.PHP.EmbeddedPhp.NoSemicolon ?> -