Support » Plugin: W3 Total Cache » How to change minified css file name?

  • ChecK995

    (@check995)


    Does anybody know how to generate a new file name for the combined css files?
    My problem is that users which doesnt clear their browser cache will see the old css
    styled page.

    Also after purging all caches the file name is still the same.

    Thanks for your help…
    Lukas

Viewing 15 replies - 1 through 15 (of 16 total)
  • Plugin Contributor gidomanders

    (@gidomanders)

    On clicking Purge All Caches, Minify should automatically regenerate and change filenames for CSS. You might want to change the Cache Control Policy for HTML & XML in Browser Cache to something like Cache with max-age and validation, because the pages could be in browser cache and you cannot remotely purge that.

    I have the same problem.
    The content of minified files is updated, but the address always remains the same
    /wp-content/cache/minify/6355c.css

    Tried to change Cache Control Policy… with no luck =(

    It happens for me as well, the filename doesn’t changed when I purge all cache. I ended up add a prefix to the cache filename based on theme version.

    /**
      * Define Our Themes Version
      * this good if we update something and prevent old user browser cache
     */
    global $themes_version;
    $themes_version = '2.1.2'; // Change this version everytime you change your css/js assets 
    
    add_filter('w3tc_minify_urls_for_minification_to_minify_filename', 'w3tc_filename_filter', 20, 3);
    function w3tc_filename_filter($minify_filename, $files, $type ){
        
        global $themes_version;
        
        $ver = sanitize_title( str_replace('.','', $themes_version) );
        
        $minify_filename = $ver.$minify_filename;
        
        return $minify_filename;
        
    }

    Hope it helps someone

    Thanks for the fix hunio! This seems like something that should be handled better within W3TC but your fix works beautifully.

    Hi,

    I’m having the same issue.

    Unfortunately, Cache with max-age and validation does not work if the user has already cached the file.

    I’m using hunio’s approach, but I’m going to generate an md5 footprint of $files.

    I’m asking to plugin developers if it is possible to add an md5 footprint of the minified file to the filename, that will save a lot of headaches.

    Minified css file:
    body{color:black}

    Filename: c6e4b.81c7deb790e3bd97c67eb2a3a77c5b69.css

    Minified css file:
    body{color:red}

    Filename: c6e4b.6700e3e577966de91432a219303a54ce.css

    • This reply was modified 11 months ago by giti.
    • This reply was modified 11 months ago by giti.
    Plugin Contributor gidomanders

    (@gidomanders)

    @giti Indeed if the user has already cached a file with the wrong browser cache headers, then it doesn’t work, but nothing else will either. The user can clear the browser cache or the cache will expire after a while, only then the new headers will work for those users. For new users though, changing the setting will cause their browsers to display an always up-to-date website.

    this is really annoying issue, every time I clear the cache, the same css and js files are being referenced in the head and file names are not being changed.

    Thanks @hunio! your solution worked fine!

    @gidomanders The problem is, that you are not generating new names for minified files. If you would rename them, the user would be forced to download them again (even if the old one is cached in his browser). This is called cache busting and would be a nice feature for W3TC. That’s what the solution fro @hunio does, thx for that.

    ainteriorb

    (@ainteriorb)

    Very good summary @tiiunder ! Definitly a feature request! Our customers always have outdated css files after quick changes.

    advcha

    (@advcha)

    Hi. we got the same problem. Here is how we solved the unremoved cache problem. On the W3TC admin setting, pls go to ‘Browser cache’ setting then Check/Enable the option ‘Prevent caching of objects after settings change’. It’d add a suffix on the minified file.

    Thx @advcha,
    this does the trick without altering the functions.php.

    Heitor_tito

    (@heitor_tito)

    Hello

    the only solution that worked for me was @hunio, which is doing exactly what the plugin should do.
    In my opinion, after resetting the cache the file name needs to be changed, regardless of whether the user has already downloaded the previous file or not.

    Hi everyone thanks to the code of @hunio and studying the original of W3tc I did a snippet that generate a filename based on the content checksum of all the files that will be merged (in that case).
    In this way the cache will be invalidate if there are any changes, right now W3TC for the name use the files name and this is not very useful.

    I uploaded the snippet on gist https://gist.github.com/Mte90/f54dd4d47571d54bc3ae76690020f14b

    I had js errors with Daniele’s version. And i didn’t want use theme version (i developed a theme and if a save an option i don’t change theme version) so i used this code

    add_filter('w3tc_minify_urls_for_minification_to_minify_filename', 'w3tc_filename_filter', 20, 3);
    function w3tc_filename_filter($minify_filename, $files, $type ){
        $vers = "v_".date("Y_m_d_H_i_s")."_";
        $minify_filename = $vers.$minify_filename;
        return $minify_filename;
    }
    rddigital

    (@rddigital)

    I modified the code, because it loaded every time a different filename

    add_action( 'w3tc_flush_all', 'action_w3tc_flush_all', 10, 1 ); 
    function action_w3tc_flush_all(){
    	$vers = "v_".date("Y_m_d_H_i_s")."_";
    	update_option( "w3_files_version", $vers);
    }
    
    add_filter('w3tc_minify_urls_for_minification_to_minify_filename', 'w3tc_filename_filter', 20, 3);
    function w3tc_filename_filter($minify_filename, $files, $type ){
        $vers = get_option("w3_files_version");
        if($vers == NULL || $vers == ""){
    		$vers = "v_".date("Y_m_d_H_i_s")."_";
    		update_option( "w3_files_version", $vers);
    	}
        $minify_filename = $vers.$minify_filename;
        return $minify_filename;
    }
Viewing 15 replies - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.