Skip to content

Commit

Permalink
Use plugins_url() to allow Workbox script URLs to be filtered
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Aug 23, 2021
1 parent 49603fe commit df1598b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public function get_script() {
$current_scope = wp_service_workers()->get_current_scope();
$workbox_dir = sprintf( 'wp-includes/js/workbox-v%s/', PWA_WORKBOX_VERSION );

$workbox_dir_url = plugins_url(
$workbox_dir,
PWA_PLUGIN_FILE
);

$script = '';
if ( SCRIPT_DEBUG ) {
$enable_debug_log = defined( 'WP_SERVICE_WORKER_DEBUG_LOG' ) && WP_SERVICE_WORKER_DEBUG_LOG;
Expand All @@ -64,7 +69,7 @@ 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.
Expand All @@ -74,7 +79,7 @@ public function get_script() {

$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
12 changes: 7 additions & 5 deletions wp-includes/service-workers.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,13 @@ 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'
$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

0 comments on commit df1598b

Please sign in to comment.