Skip to content

Commit

Permalink
Utilize workbox-window to register SW; eliminate wordpress_sw_install…
Browse files Browse the repository at this point in the history
…ed cookie
  • Loading branch information
westonruter committed Jul 26, 2021
1 parent 591c7a3 commit 4c3da17
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions wp-includes/service-workers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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
?>
<script>
if ( navigator.serviceWorker ) {
window.addEventListener( 'load', function() {
<?php foreach ( $scopes as $name => $scope ) : ?>
{
navigator.serviceWorker.register(
<?php echo wp_json_encode( wp_get_service_worker_url( $name ) ); ?>,
<?php echo wp_json_encode( compact( 'scope' ) ); ?>
).then( reg => {
<?php if ( WP_Service_Workers::SCOPE_ADMIN === $name ) : ?>
document.cookie = <?php echo wp_json_encode( sprintf( 'wordpress_sw_installed=1; path=%s; expires=Fri, 31 Dec 9999 23:59:59 GMT; secure; samesite=strict', $scope ) ); ?>;
<?php endif; ?>
} );
}
<?php endforeach; ?>
} );
<script type="module">
import { Workbox } from <?php echo wp_json_encode( $workbox_window_src ); ?>;

// @todo Defer to load event?
if ( 'serviceWorker' in navigator ) {
window.wp = window.wp || {};
window.wp.serviceWorkerWindow = new Workbox(
<?php echo wp_json_encode( $sw_src ) ?>,
<?php echo wp_json_encode( $register_options ) ?>
);
// @todo Allow clients to register event listeners here.
window.wp.serviceWorkerWindow.register();
}

</script>
<?php
// phpcs:enable Squiz.PHP.EmbeddedPhp.NoSemicolon
}

/**
Expand Down

0 comments on commit 4c3da17

Please sign in to comment.