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

Use plugins_url() to allow Workbox script URLs to be filtered #600

Merged
merged 2 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ public function get_priority() {
* @return string Script.
*/
public function get_script() {
$current_scope = wp_service_workers()->get_current_scope();
$workbox_dir = sprintf( 'wp-includes/js/workbox-v%s/', PWA_WORKBOX_VERSION );
$current_scope = wp_service_workers()->get_current_scope();
$workbox_dir_path = sprintf( 'wp-includes/js/workbox-v%s/', PWA_WORKBOX_VERSION );
$workbox_dir_url = plugins_url( $workbox_dir_path, PWA_PLUGIN_FILE ); // Core merge: replace with includes_url().

$script = '';
if ( SCRIPT_DEBUG ) {
Expand All @@ -64,17 +65,18 @@ public function get_script() {
// Load with importScripts() so that source map is available.
$script .= sprintf(
"importScripts( %s );\n",
wp_json_encode( PWA_PLUGIN_URL . $workbox_dir . 'workbox-sw.js' )
wp_json_encode( $workbox_dir_url . 'workbox-sw.js' )
);
} else {
// Inline the workbox-sw.js to avoid an additional HTTP request.
$wbjs = file_get_contents( PWA_PLUGIN_DIR . '/' . $workbox_dir . 'workbox-sw.js' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
// Core merge: Replace with PWA_PLUGIN_DIR with ABSPATH . WPINC . etc.
$wbjs = file_get_contents( PWA_PLUGIN_DIR . '/' . $workbox_dir_path . 'workbox-sw.js' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$script .= preg_replace( '://# sourceMappingURL=.+?\.map\s*$:s', '', $wbjs );
}

$options = array(
'debug' => SCRIPT_DEBUG, // When true, the dev builds are loaded. Otherwise, the prod builds are used.
'modulePathPrefix' => PWA_PLUGIN_URL . $workbox_dir,
'modulePathPrefix' => $workbox_dir_url,
);
$script .= sprintf( "workbox.setConfig( %s );\n", wp_json_encode( $options ) );

Expand Down Expand Up @@ -122,6 +124,7 @@ public function get_script() {
}

// Note: This includes the aliasing of `workbox` to `wp.serviceWorker`.
// Core merge: Replace with PWA_PLUGIN_DIR with ABSPATH . WPINC . etc.
$script .= file_get_contents( PWA_PLUGIN_DIR . '/wp-includes/js/service-worker.js' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents

return $script;
Expand Down
13 changes: 8 additions & 5 deletions wp-includes/service-workers.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,14 @@ function wp_print_service_workers() {
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'
// Core merge: replace plugins_url() with includes_url().
$workbox_window_src = plugins_url(
sprintf(
'wp-includes/js/workbox-v%s/workbox-window.%s.js',
PWA_WORKBOX_VERSION,
SCRIPT_DEBUG ? 'dev' : 'prod'
),
PWA_PLUGIN_FILE
);
$register_options = array(
'scope' => $scope,
Expand Down