Skip to content

Commit

Permalink
[Boost] Change _static dir for minify (#31631)
Browse files Browse the repository at this point in the history
* Update default directory to _jb_static

* Allow overrideable CSS/JS prefix

* changelog

* use jetpack_boost_get_static_prefix instead of a static path

* Add a comment explaining the inline use of JETPACK_BOOST_STATIC_PREFIX

* Ensure path prefix is surrounded by /slashes/

---------

Co-authored-by: Mark George <thingalon@gmail.com>
  • Loading branch information
thingalon and Mark George committed Jun 30, 2023
1 parent c1677fb commit 49eeaaf
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion projects/plugins/boost/app/lib/minify/Concatenate_CSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public function do_items( $handles = false, $group = false ) {
}
}

$href = $siteurl . '/_static/??' . $path_str;
$href = $siteurl . jetpack_boost_get_static_prefix() . '??' . $path_str;
} else {
$href = jetpack_boost_page_optimize_cache_bust_mtime( current( $css ), $siteurl );
}
Expand Down
2 changes: 1 addition & 1 deletion projects/plugins/boost/app/lib/minify/Concatenate_JS.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public function do_items( $handles = false, $group = false ) {
}
}

$href = $siteurl . '/_static/??' . $path_str;
$href = $siteurl . jetpack_boost_get_static_prefix() . '??' . $path_str;
} elseif ( isset( $js_array['paths'] ) && is_array( $js_array['paths'] ) ) {
$href = jetpack_boost_page_optimize_cache_bust_mtime( $js_array['paths'][0], $siteurl );
}
Expand Down
19 changes: 17 additions & 2 deletions projects/plugins/boost/app/lib/minify/functions-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,21 @@ function jetpack_boost_page_optimize_cache_bust_mtime( $path, $siteurl ) {
}

/**
* Detects requests within the `/_static/` directory, and serves minified content.
* Get the URL prefix for static minify/concat resources. Defaults to /jb_static/, but can be
* overridden by defining JETPACK_BOOST_STATIC_PREFIX.
*/
function jetpack_boost_get_static_prefix() {
$prefix = defined( 'JETPACK_BOOST_STATIC_PREFIX' ) ? JETPACK_BOOST_STATIC_PREFIX : '/_jb_static/';

if ( substr( $prefix, 0, 1 ) !== '/' ) {
$prefix = '/' . $prefix;
}

return trailingslashit( $prefix );
}

/**
* Detects requests within the `/_jb_static/` directory, and serves minified content.
*
* @return void
*/
Expand All @@ -270,7 +284,8 @@ function jetpack_boost_minify_serve_concatenated() {
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$request_path = explode( '?', wp_unslash( $_SERVER['REQUEST_URI'] ) )[0];
if ( '/_static/' === substr( $request_path, -9, 9 ) ) {
$prefix = jetpack_boost_get_static_prefix();
if ( $prefix === substr( $request_path, -strlen( $prefix ), strlen( $prefix ) ) ) {
require_once __DIR__ . '/functions-service.php';
jetpack_boost_page_optimize_service_request();
exit;
Expand Down
10 changes: 5 additions & 5 deletions projects/plugins/boost/app/lib/minify/functions-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function jetpack_boost_page_optimize_types() {
}

/**
* Handle serving a minified / concatenated file from the virtual _static dir.
* Handle serving a minified / concatenated file from the virtual _jb_static dir.
*/
function jetpack_boost_page_optimize_service_request() {
$use_wp = defined( 'JETPACK_BOOST_CONCAT_USE_WP' ) && JETPACK_BOOST_CONCAT_USE_WP;
Expand Down Expand Up @@ -153,9 +153,9 @@ function jetpack_boost_page_optimize_build_output() {
}

// Ensure the path follows one of these forms:
// /_static/??/foo/bar.css,/foo1/bar/baz.css?m=293847g
// /_jb_static/??/foo/bar.css,/foo1/bar/baz.css?m=293847g
// -- or --
// /_static/??-eJzTT8vP109KLNJLLi7W0QdyDEE8IK4CiVjn2hpZGluYmKcDABRMDPM=
// /_jb_static/??-eJzTT8vP109KLNJLLi7W0QdyDEE8IK4CiVjn2hpZGluYmKcDABRMDPM=
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? $utils->unslash( $_SERVER['REQUEST_URI'] ) : '';
$args = $utils->parse_url( $request_uri, PHP_URL_QUERY );
Expand All @@ -166,7 +166,7 @@ function jetpack_boost_page_optimize_build_output() {
$args = substr( $args, strpos( $args, '?' ) + 1 );

// Detect paths with - in their filename - this implies a base64 encoded gzipped string for the file list.
// e.g.: /_static/??-eJzTT8vP109KLNJLLi7W0QdyDEE8IK4CiVjn2hpZGluYmKcDABRMDPM=
// e.g.: /_jb_static/??-eJzTT8vP109KLNJLLi7W0QdyDEE8IK4CiVjn2hpZGluYmKcDABRMDPM=
if ( '-' === $args[0] ) {
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
$args = @gzuncompress( base64_decode( substr( $args, 1 ) ) );
Expand Down Expand Up @@ -199,7 +199,7 @@ function jetpack_boost_page_optimize_build_output() {
// We can't assume that the root serves the same content as the subdir.
$subdir_path_prefix = '';
$request_path = $utils->parse_url( $request_uri, PHP_URL_PATH );
$_static_index = strpos( $request_path, '/_static/' );
$_static_index = strpos( $request_path, jetpack_boost_get_static_prefix() );
if ( $_static_index > 0 ) {
$subdir_path_prefix = substr( $request_path, 0, $_static_index );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Minify CSS/JS: Added a new way for site owners to override the default _jb_static/ path prefix for Boost's Minified CSS and JS URLs
5 changes: 4 additions & 1 deletion projects/plugins/boost/jetpack-boost.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$request_path = explode( '?', wp_unslash( $_SERVER['REQUEST_URI'] ) )[0];
if ( '/_static/' === substr( $request_path, -9, 9 ) ) {

// Handling JETPACK_BOOST_STATIC_PREFIX constant inline to avoid loading the minify module until we know we want it.
$static_prefix = defined( 'JETPACK_BOOST_STATIC_PREFIX' ) ? JETPACK_BOOST_STATIC_PREFIX : '/_jb_static/';
if ( $static_prefix === substr( $request_path, -strlen( $static_prefix ) ) ) {
define( 'JETPACK_BOOST_CONCAT_USE_WP', true );

require_once JETPACK_BOOST_DIR_PATH . '/serve-minified-content.php';
Expand Down

0 comments on commit 49eeaaf

Please sign in to comment.