From 23b7f253c4687d6b42cdc136f9bbc8c35f8dd484 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 26 Oct 2018 21:11:12 -0700 Subject: [PATCH] Inline workbox-sw.js in non-WP_DEBUG to eliminate HTTP request --- ...-wp-service-worker-configuration-component.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/wp-includes/components/class-wp-service-worker-configuration-component.php b/wp-includes/components/class-wp-service-worker-configuration-component.php index 52f4b589c..82949fe3a 100644 --- a/wp-includes/components/class-wp-service-worker-configuration-component.php +++ b/wp-includes/components/class-wp-service-worker-configuration-component.php @@ -54,10 +54,17 @@ public function get_script() { $current_scope = wp_service_workers()->get_current_scope(); $workbox_dir = 'wp-includes/js/workbox-v3.6.1/'; - $script = sprintf( - "importScripts( %s );\n", - wp_service_worker_json_encode( PWA_PLUGIN_URL . $workbox_dir . 'workbox-sw.js' ) - ); + if ( WP_DEBUG ) { + // Load with importScripts() so that source map is available. + $script = sprintf( + "importScripts( %s );\n", + wp_service_worker_json_encode( PWA_PLUGIN_URL . $workbox_dir . 'workbox-sw.js' ) + ); + } else { + // Inline the workbox-sw.js to avoid an additional HTTP request. + $script = file_get_contents( PWA_PLUGIN_DIR . '/' . $workbox_dir . 'workbox-sw.js' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents + $script = preg_replace( '://# sourceMappingURL=.+?\.map:', '', $script ); + } $options = array( 'debug' => WP_DEBUG,