Hi together,
I’m searching the best solution for caching the pages for different devices. I need it because we have different menu snippets for mobile and desktop. For detecting the devices I use the plugin Detect from the respository.
Failed test:
Adding this to config.php to change the directory like the dev guide
$device = s::get( 'device_class' );
if( $device == 'mobile' ) {
c::set('cache.root', 'mobile_cache');
}
else if( $device == 'tablet' ) {
c::set('cache.root', 'tablet_cache');
} else {
c::set('cache.root', 'desktop_cache');
}
Any idea to solve?
Thx
The problem is the load order, the configuration is called before the detect plugin, so it won’t ever know of the value of the session variable.You could try to load the detect class in the config (instead of using the plugin).
Thanks for your hint.
I test it with the dump function.
$device = s::get( 'device_class' );
if( $device == 'mobile' ) {
dump( $device );
c::set('cache.root', 'mobile_cache');
} else if( $device == 'tablet' ) {
dump( $device );
c::set('cache.root', 'tablet_cache');
} else {
c::set('cache.root', 'desktop_cache');
}
I get the right dump on mobile and tablet. So the session is loaded.
Am I missing something?
The same result by loading the decect class in the config.
I get the right dump in the frontend.
require_once( __DIR__ . DS . '..' . DS . 'plugins' . DS . 'detect' . DS . 'lib' . DS . 'Mobile_Detect.php');
$detect = new Mobile_Detect();
if( $detect->isMobile() && !$detect->isTablet() ) {
dump('mobile');
c::set('cache.root', 'cache/mobile');
}
else if( $detect->isTablet() ) {
dump('tablet');
c::set('cache.root', 'cache/tablet');
} else {
c::set('cache.root', 'cache/desktop');
}
How does the path look like?
c::set('cache.root', '/usr/www/users/xxx/kirby/site/cache/desktop');
or
c::set('cache.root', 'cache/desktop');
Hm, I’m not sure, in Kirby 2.4.0 beta, you can have a custom folder layout for all folders in site and could do this in site.php
site.php
<?php
$kirby = kirby();
$device = s::get( 'device_class' );
if( $device == 'mobile' ) {
$kirby->roots->cache = kirby()->roots()->index() . DS . 'mobile-cache';
} else if ( $device == 'tablet' ) {
$kirby->roots->cache = kirby()->roots()->index() . DS . 'tablet-cache';
} else {
$kirby->roots->cache = kirby()->roots()->index() . DS . 'site/cache';
}
Edit: The docs are not correct, the option is called cache.options
and the syntax should be like this:
c::set('cache', true);
$device = s::get( 'device_class' );
if( $device == 'mobile' ) {
c::set('cache.options', array(
'root' => kirby()->roots()->index() . DS . 'mobile-cache'
)
);
} else if( $device == 'tablet' ) {
c::set('cache.options', array(
'root' => kirby()->roots()->index() . DS . 'tablet-cache'
)
);
} else {
c::set('cache.options', array(
'root' => kirby()->roots()->index() . DS . 'desktop-cache'
)
);
}
2 Likes
Perfect!!! Thx a lot.
You have saved my idea of page speed 