Anyone using the Mobile Detect Plugin?

I’m very interested in using this plugin, and noticed a fairly critical seeming issue on the Github page here stating an issue with Grav’s caching.

There seems to be an issue when using Grav's page caching.

When enabled the {{ isMobile() }} and {{ isTablet() }} vars only work intermittently on mobiles/tablets and even intermittently gives a false positive resulting in mobile style sheets etc being loaded for desktop browsers.

I haven’t had a chance to play with the plugin yet, but couldn’t one conceivably just use one of the other functions as a surrogate, or do they all have issues due to Grav’s caching, for those of you who have tried it out? For example, you could potentially use

{{ mobile_detect() }} which returns tablet, phone or desktop

Look forward to hearing your thoughts!

it works great without cache. but it seems that we need a dedicated cache file for mobile devices with that condition it will work with cache activated

Hi @dimitrilongo,
Thanks for the response (and thanks for creating the Mobile Detect Plugin in the first place!). I’m admittedly not that familiar with grav’s caching methods, but it seems like without the cache, Grav would be significantly slower/less optimal? I see the point regarding a mobile-specific cache file being necessary - that makes sense.

you’re right, that’s why i posted the issue and ask for help, cause i’m not familiar with all caches methods. I hope someone see this thread or the issue to improve the plugin because with this PHP class you can do lot of things, improve user s experience, target audience, etc

@Rhukster or @flaviocopes - sorry for the nooby question, but is there any way I can learn more about gravs cache methods to try implementing this myself?

This internal stuff is really not documented and the best way to learn about it is to read through the code. However, there is this Performance & Caching section in the docs.

Basically the gist is that stuff in .md files is cached, and only cleared when you clear the cach manually, or a change is detected in the timestamp of the file. A workaround is to simply disable the caching for the single page where caching is not wanted. That way you get the benefits of the rest of Grav caching, but your single page is generated each time. Often the speed of this is neglibable in real-world scenarios.

Stuff in Twig templates uses Twig’s own caching mechanism that is quite sophisticated, and basically turns Twig into PHP, so things remain dynamic.

Thanks for the response rhukster (and welcome back after the holidays)! I’ll dig in to the performance and caching information to see what I can learn. It looks like this will probably fall under the umbrella of the sophisticating twig caching, as that is where this plugin is intended for customzing twig templates further.

maybe exclude some part of the twig from cache, the part where you add condition for mobile for instance. it seems Craft CMS use a twig filter {% cache %}

Actually there is already a TwigCache plugin that does exactly that.

@rhukster you’re right here :wink:

This is awesome- I look forward to giving that a try with the mobile detect plugin. Thanks!

@spikesp take a look Added ability to never cache twig I didn’t test it, should work with logical in modular for instance

Thanks dimitrilongo - I’m out of town right now but will test that when back. This looks really useful even outside the realm of mobile detect. Cheers!

I never understand the concept of mobile plugin. I mean if you can make your site responsive to like 200px. Why do you need mobile plugin? I was able to make my to do just that without mobile plugin. A good contact form plugin is what we need.

Sometimes you want to serve different HTML or content to mobile clients. That’s where it comes in handy.