• mclaurent

    (@mclaurent)


    Hi

    We’re using the Bridge theme which conditionnally enqueues styles for different User agents. So for instance, it might enqueue a stylesheet specifically for safari, like so:

    
    if($is_safari) {
     wp_enqueue_style("safari", THEME_ROOT . "/css/safari_stylesheet.css");
    }
    

    Now when I enable SuperCache, it will cache the HTML and serve it to multiple requests. The issue is there if I visit the site on Safari, SuperCache will save the site including the Safari-specific stylesheet. If I visit the site on Chrome, the menus don’t behave as expected because there is CSS included that shouddn’t be there.
    Is it possible to tell SuperCache to create a cached version per user agent?
    So if I visit the site on Safari, SuperCache will retuyrn a cached version only if the cached version matches my user agent?

    Thanks

    • This topic was modified 6 years ago by mclaurent.
    • This topic was modified 6 years ago by mclaurent.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Sa?a

    (@stodorovic)

    You should create WPSC plugin which can create separate cache file (wp-cache instead of super-cache file in this case) based on user agent. Also, you should set “Simple” instead of “Expert” delivery method.

    Regarding to WPSC plugin, you need to add cache action (filter) wp_cache_key which should be unique ‘key’ for cache file. Something like this:

    add_cacheaction( 'wp_cache_key', 'my_custom_ua_key' );
    
    function my_custom_ua_key( $key ) {
    	if ( ! empty($_SERVER['HTTP_USER_AGENT']) && stripos( $_SERVER['HTTP_USER_AGENT'], 'safari' ) !== false ) {
    		$key .= 'safari';
    		$super_cache_enabled = false; // Use wp-cache file.
    	}
    
    	return $key;
    }
    

    It can’t be added in plugins or theme because it should be executed early before loading plugins. You can read more on https://odd.blog/2017/10/25/writing-wp-super-cache-plugins/.

    Thread Starter mclaurent

    (@mclaurent)

    Heya

    Many thanks for your reply (and so quickly, too!). That sounds like it could do the job, I’ll give it a try as soon as I can and let you know how it went.

    Cheers

    Thread Starter mclaurent

    (@mclaurent)

    Hello

    Thanks again. I have tried but I have had to tweak it a bit to get it to work. In particular, when the cache file didn’t exist, it would attempt to use the supercache_filename_str filter hook to generate the file name to be used. I have tested this on a number of browsers (Chrome, Ie, Safari) and seems to work alright. If you do have any feedback on the below, that would be much appreciated! I saved the below in wp-contents/plugins/wp-super-cache-plugins/caching-by-user-agent.php which did the trick.

    
    <?php
    
    add_filter( 'supercache_filename_str', 'my_custom_ua_key' );
    
    function my_custom_ua_key($key) {
    	global $is_safari, $is_chrome, $is_IE;
    
    	if ( isset( $is_safari) && $is_safari ) {
    		$key .= '-safari';
    	} elseif ( isset( $is_chrome) && $is_chrome ) {
    		$key .= '-chrome';
    	} elseif ( isset( $is_chrome) && $is_IE ) {
    		$key .= '-ie';
    	}
    
    	return $key;
    }
    
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Caching by user agent’ is closed to new replies.