Skip to content

Boost: Update minify garbage collection to clean up files as well #44137

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

Open
wants to merge 5 commits into
base: add/boost/minify-cleanup-HOG-189
Choose a base branch
from
Open
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 @@ -103,6 +103,7 @@ public function cleanup_stored_paths_batch() {
}

if ( $value['expire'] <= time() ) {
$this->delete_static_file_by_hash( str_replace( 'jb_transient_concat_paths_', '', $option['option_name'] ) );
delete_option( $option['option_name'] );
}

Expand All @@ -114,6 +115,27 @@ public function cleanup_stored_paths_batch() {
return true;
}

/**
* Deletes the static files by hash.
*
* @param string $hash The hash of the file.
* @return void
*/
private function delete_static_file_by_hash( $hash ) {
// Since we don't have a way to know if this file is JS or CSS,
// we delete both.

$js_file_path = jetpack_boost_get_minify_file_path( $hash . '.min.js' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just confirming here; It's not possible/extremely unlikely that there can be both a $hash.min.js, and a $hash.min.css? As if both exist, the CSS file will not be deleted

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hadn't thought of that.

Though the hash is built from the path names of the assets and the modify time of the newest one.

I've updated this in 96338bd.

if ( file_exists( $js_file_path ) ) {
wp_delete_file( $js_file_path );
}

$css_file_path = jetpack_boost_get_minify_file_path( $hash . '.min.css' );
if ( file_exists( $css_file_path ) ) {
wp_delete_file( $css_file_path );
}
}

/**
* Gets the stored paths to process, and skips the ones that were checked in the previous run.
* Also sets a flag that determines if there should be a followup cleanup or not.
Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/boost/app/lib/minify/functions-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ function jetpack_boost_get_minify_url( $file_name = '' ) {
return content_url( '/boost-cache/static/' . $file_name );
}

function jetpack_boost_get_minify_file_path( $file_name = '' ) {
return WP_CONTENT_DIR . '/boost-cache/static/' . $file_name;
}

/**
* Detects requests within the `/_jb_static/` directory, and serves minified content.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Concatenate JS/CSS: Cleanup static files when running garbage collection.