I think your problem is in the framework you intended for the cache, believe me, I have a structure similar to yours that I use on my clients' websites, what I did to resolve was to add a suffix when mobile and another when for desktop, so avoid loading the backend cache into the different user-agent type
That is, the suffix solution serves to differentiate between static and mobile to Desktop. Of course you can do a JavaScript approach, here I am just presenting how to solve what you already have.
The other suggestion would be to use media-queries and resolve everything via CSS, but in this case you would have to reshape HTML as well.
It was something like:
global.php
<?php
function is_mobile() {
//Se não tiver user-agent definido retorna false/assume como desktop
if (empty($_SERVER['HTTP_USER_AGENT'])) return false;
//Com regex checa as palavras que possivelmente o usuário esta acessando via celular
return preg_match('#iPhone|Android|webOS#', $_SERVER['HTTP_USER_AGENT']) > 0;
}
//Cache de 3600 segundos/mesmo que 1 hora de cache
$cachelimite = 3600;
//Sufixo para Desktop/padrão
$sufixo = '.desktop.html';
if (is_mobile()) {
//Troca o sufixo se for celular
$sufixo = '.mobile.html';
}
//Define a pasta aonde deve ser salvo os estáticos, você pode ajustar
$cachefolder = 'cachepages/';
//Captura a URL com querystring
$url = $_SERVER['PHP_SELF'];
//Gera o caminho do arquivo
$cachefile = realpath($cachefolder . strlen($url) . '_' . sha1($url) . $sufixo);
//verifica se o cache existe e se expirou o tempo limite
if (is_file($cachefile) && filemtime($cachefile) + $cachelimite > time()) {
readfile($cachefile);
exit;
}
//Inicia a captura do buffer
ob_start();
register_shutdown_function(function () use ($cachefile) {
//gravar o buffer no arquivo estático
file_put_contents($cachefile, ob_get_contents());
});
Note that sha1
there was just an example, just as md5 sha1 can also conflict, the idea is just a basic example to understand the logic, you can adapt later.
Pages receive this:
<?php
require_once 'global.php';
So what solves the problem of loading the cache is this:
$sulfixo = '.desktop.html';
if (is_mobile()) {
$sulfixo = '.mobile.html';
}
Of course I did a simplified regex to detect the user-agent:
return preg_match('#iPhone|Android|webOS#', $_SERVER['HTTP_USER_AGENT']) > 0;
But it's just to illustrate, in case you can improve it or adapt it to your need, using the pipe |
tab in the regular expression.