• Resolved JacobTheDev

    (@revxx14)


    I’ve got a bit of an issue with this plugin when it comes to caching in certain browsers.

    I use a bit of browser sniffing to serve up some things only to specific browsers. Some examples:

    – On iOS devices, touch icons get added to the header
    – In Chrome, <meta name="theme-color" content="#222222" /> gets added to the header
    – In IE, a “legacy” stylesheet gets added to fix a few IE specific bugs

    Most of these I could just serve to all browsers without issue, but that last one is the real problem. When someone visits the site in IE, and cache gets regenerated, the IE version of the page gets served to all subsequent users. This causes a lot of weird and unexpected behavior, primarily breaking certain responsive features.

    How can I tell WP Super Cache “if it’s IE, don’t use this request to cache” ?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter JacobTheDev

    (@revxx14)

    Okay, I did some R&D, and I’ve discovered a solution for my problem.

    First, I need to add “MSIE” and “Trident” to the list under Settings > WP Super Cache > Advanced > Rejected User Agents. I can do this manually, but the better solution for my use case is automatically. I’ve written a small function that adds the two variables to the array, and it works beautifully.

    /**
     * Add <code>MSIE</code> and <code>Trident</code> to rejected user agents
     *
     * @return void
     */
    function prefix_wp_super_cache_disable_ie(): void {
        global $cache_rejected_user_agent;
    
        if ($cache_rejected_user_agent) {
            $cache_rejected_user_agent[] = "MSIE";
            $cache_rejected_user_agent[] = "Trident";
        }
    }
    add_action("init", "prefix_wp_super_cache_disable_ie");

    Second, I need to change define('WP_CACHE', true) in wp-config.php so that it doesn’t apply if the user is visiting from IE. This is simple enough to do by editing the wp-config.php directly, but ideally I’d like to do this automatically via a filter.

    if (!(stripos($_SERVER["HTTP_USER_AGENT"], "MSIE") || stripos($_SERVER["HTTP_USER_AGENT"], "Trident"))) {
        define('WP_CACHE', true);
    }

    This does exactly what I want it to do, but I have to set it up manually on every site I want to do this on. I see that this line gets added in wp-cache.php at lines 2583-2584. There is no filter on this, so I can’t modify it via my theme.

    Is there any chance you guys could add a filter, something like supercache_wp_config_line, so that developers can modify it as needed? This would be immensely helpful for me.

    Thread Starter JacobTheDev

    (@revxx14)

    I replied on Github. You can create a WP Super Cache plugin. It loads very early and you check the user agent there and disable caching.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How can I blacklist caching for certain browsers?’ is closed to new replies.