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

Utilize workbox-window for service worker registration #578

Merged
merged 9 commits into from
Aug 24, 2021
10 changes: 9 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ module.exports = function (grunt) {
},
install_workbox: {
command:
'if [ -e wp-includes/js/workbox* ]; then rm -r wp-includes/js/workbox*; fi; npx workbox copyLibraries wp-includes/js/',
'if [ -e wp-includes/js/workbox* ]; then ' +
'rm -r wp-includes/js/workbox*; ' +
'fi; ' +
'npx workbox copyLibraries wp-includes/js/;' +
// Rename .mjs with .js since servers may not be configured to serve the former as text/javascript.
'for mjs in wp-includes/js/workbox*/*.mjs; do ' +
'sed "s/\\.mjs/.js/g" "$mjs" > "${mjs%mjs}js";' +
'rm $mjs;' +
'done;',
},
create_build_zip: {
command:
Expand Down
2 changes: 0 additions & 2 deletions wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,3 @@
add_action( 'wp_head', 'wp_add_error_template_no_robots' );
add_action( 'error_head', 'wp_add_error_template_no_robots' );
}

add_action( 'admin_init', 'wp_disable_script_concatenation' );
28 changes: 28 additions & 0 deletions wp-includes/deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,31 @@ function wp_service_worker_json_encode( $data ) {
_deprecated_function( __FUNCTION__, '0.6', 'wp_json_encode()' );
return wp_json_encode( $data, 128 | 64 /* JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES */ );
}

/**
* Disables concatenating scripts to leverage caching the assets via Service Worker instead.
*
* @deprecated 0.7 No longer used.
* @codeCoverageIgnore
*/
function wp_disable_script_concatenation() {
_deprecated_function( __FUNCTION__, '0.7' );

global $concatenate_scripts;

/*
* This cookie is set when the service worker registers successfully, avoiding unnecessary result
* for browsers that don't support service workers. Note that concatenation only applies in the admin,
* for authenticated users without full-page caching.
*/
if ( isset( $_COOKIE['wordpress_sw_installed'] ) ) {
$concatenate_scripts = false; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
}

// phpcs:disable
// @todo This is just here for debugging purposes.
if ( isset( $_GET['wp_concatenate_scripts'] ) ) {
$concatenate_scripts = rest_sanitize_boolean( $_GET['wp_concatenate_scripts'] );
}
// phpcs:enable
}
10 changes: 9 additions & 1 deletion wp-includes/js/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ wp.serviceWorker = workbox;
* mechanism for clients to skip waiting if they want to.
*/
self.addEventListener('message', function (event) {
if ('skipWaiting' === event.data.action) {
if (!event.data) {
return;
}
if (
// De facto standard used by Workbox.
event.data.type === 'SKIP_WAITING' ||
// Obsolete message sent in older versions of the plugin.
'skipWaiting' === event.data.action
) {
self.skipWaiting();
}
});
76 changes: 29 additions & 47 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,37 +151,43 @@ 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,
);

?>
<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 ); ?>;

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 ); ?>
);
window.wp.serviceWorkerWindow.register();
}
</script>
<?php
Expand Down Expand Up @@ -228,29 +233,6 @@ function wp_ajax_wp_service_worker() {
die();
}

/**
* Disables concatenating scripts to leverage caching the assets via Service Worker instead.
*/
function wp_disable_script_concatenation() {
global $concatenate_scripts;

/*
* This cookie is set when the service worker registers successfully, avoiding unnecessary result
* for browsers that don't support service workers. Note that concatenation only applies in the admin,
* for authenticated users without full-page caching.
*/
if ( isset( $_COOKIE['wordpress_sw_installed'] ) ) {
$concatenate_scripts = false; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
}

// phpcs:disable
// @todo This is just here for debugging purposes.
if ( isset( $_GET['wp_concatenate_scripts'] ) ) {
$concatenate_scripts = rest_sanitize_boolean( $_GET['wp_concatenate_scripts'] );
}
// phpcs:enable
}

/**
* Checks if Service Worker should skip waiting in case of update and update automatically.
*
Expand Down