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

Allow for Workbox.js scripts to be loaded from CDN #543

Closed
westonruter opened this issue Jun 18, 2021 · 3 comments · Fixed by #600
Closed

Allow for Workbox.js scripts to be loaded from CDN #543

westonruter opened this issue Jun 18, 2021 · 3 comments · Fixed by #600
Milestone

Comments

@westonruter
Copy link
Collaborator

As requested on a support forum topic, some sites may want to load the Workbox scripts from a CDN instead of loading them from origin.

This is not currently supported, as seen here:

$workbox_dir = sprintf( 'wp-includes/js/workbox-v%s/', PWA_WORKBOX_VERSION );
$script = '';
if ( SCRIPT_DEBUG ) {
$enable_debug_log = defined( 'WP_SERVICE_WORKER_DEBUG_LOG' ) && WP_SERVICE_WORKER_DEBUG_LOG;
if ( ! $enable_debug_log ) {
$script .= "self.__WB_DISABLE_DEV_LOGS = true;\n";
}
// 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' )
);
} 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
$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,
);
$script .= sprintf( "workbox.setConfig( %s );\n", wp_json_encode( $options ) );

Perhaps this could be supported by allowing $options to be filtered in PHP.

@westonruter westonruter added service-workers enhancement New feature or request labels Jun 18, 2021
@westonruter
Copy link
Collaborator Author

Eventually this could be achieved by passing the paths through the includes_url() function so that it could be filtered.

@westonruter westonruter added this to the 0.7 milestone Aug 23, 2021
@westonruter
Copy link
Collaborator Author

For now, we can just make use of the plugins_url() function so that the plugins_url filter is applied to the Workbox URLs

@pooja-muchandikar
Copy link

QA Passed ✅

To cross check this issue registered a new plugin and was able to load the Workbox.js scripts through CDN, as per the URL passed in the code.

Screenshot 2022-04-20 at 6 03 05 PM


Screenshot 2022-04-20 at 5 56 15 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants