Archived topic
Remove Sidebar (completely) on mobile; cache issues
3 replies · Started by Michael on March 8, 2021
Hi,
I used this code (via codesnippets) that I found elsewhere to completely remove my sidebar on mobile.
add_filter( 'generate_sidebar_layout', function( $layout ) {
// If we are viewing single post on mobile, set the sidebar
if ( is_single() && wp_is_mobile() ) {
return 'no-sidebar';
}
// Or else, set the regular layout
return $layout;
} );
The issue is that occasionally WP Rocket is caching the mobile view and showing it on desktop. I THINK that maybe it's because it's deferring that particular piece of JS that's controlling it. Does that sound right?
If so, which JS should I exclude from deferring? Which one controls the sidebar?
Hi there,
yeah thats a big issue with using the wp_is_mobile() function - any plugin or server that uses Page Caching may inadvertently serve up the the incorrect cached file. WP Rocket i believe has a separate Cache for the mobile - but before speaking with them its worth checking to see if your server is also performing Page Caching as they may conflict.
I suspect it is. I have litespeed installed on the server. And then Sucuri on top of that (set to minimal cache). Giant pain in the ass to get these all working together. Sucuri is worth it though just for the DDOS protection.
Yeah - sounds like a lot of opportunities for the element to be cached.
Some instances, such as this , it may be better to use some CSS to hide the sidebar... i know it would be nicer not to load it but i can't advise on how to overcome caching.