wp-media / wp-rocket

Performance optimization plugin for WordPress
https://wp-rocket.me
GNU General Public License v2.0
701 stars 218 forks source link

Why to Add Query String with ver=? #6907

Closed Slamik closed 1 month ago

Slamik commented 2 months ago

Hello, I would like to inquire if there is any point in adding "ver" to all cached .css files? Googlebot is going crazy and scanning all possible "?ver=" of styles.

image

CrochetFeve0251 commented 2 months ago

Hey @Slamik , As far as remember, this is related to the cache.

When clearing the cache, if we have the same name for the old and new file, then the browser from the client will potentially have the older version of that asset cached. Then it will use it instead of the new version, which would potentially break some functionality or layout on the page.

Slamik commented 2 months ago

As far as remember, this is related to the cache. Yes, that's correct. https://github.com/wp-media/wp-rocket/blob/7999ed2618d11d7fa9b63fc78a7810789072d4b9/inc/Engine/Optimization/AbstractOptimization.php#L235

But you have to agree, it's rather strange to remove the "?ver" in the basic functionality for optimization purposes and then add a version for the cache with no way to disable it. It would be great to at least have the ability to disable this through a hook.

protected function get_full_minified_url( $minified_path, $minified_url ) {
    // Apply filter to allow disabling the query string
    $add_query_string = apply_filters( 'rocket_add_minified_version_query_string', true, $minified_url );

    if ( ! $add_query_string ) {
        return $minified_url;
    }

    $file_mtime = rocket_direct_filesystem()->mtime( $minified_path );

    $version = $file_mtime ? $file_mtime : md5( $minified_url . $this->minify_key );

    return add_query_arg( 'ver', $version, $minified_url );
}
add_filter( 'rocket_add_minified_version_query_string', '__return_false' );

add_filter( 'rocket_add_minified_version_query_string', function( $add_version, $url ) {
    if ( strpos( $url, 'specific-file.css' ) !== false ) {
        return false;
    }
    return $add_version;
}, 10, 2 );
sepidev commented 1 month ago

I agree with @CrochetFeve0251

piotrbak commented 1 month ago

Hello @Slamik, To avoid adding ver query string you could disable the minification setting, since nowadays it has a minimap impact on the performance. In general, we can not deactivate it by default for all customers. If you'd like to have a filter for your specific use case, feel free to open a PR and reopen the issue. Unfortunately, we'll not be able to work on this in the upcoming months.