Cache by User-Agent using Workers

Hi all,

Recently I run a test on one of my sites with caching pages by user-agent using CF workers.
My site is using WordPress CMS and there is a cache as well that works just fine.

My manual test was just fine and when a user with a mobile user-agent entered the site I showed him the cached version of the mobile site and the same goes for desktop.

I checked that the Worker user-agent rule for mobile identification is the same rule I have in my CMS platform. but for some reason that I can’t understand the mechanism is not working properly at all and a lot of my mobile users got a desktop version.

I suspect something may be wrong with my rule - did someone come across the issue?

here is my worker code:
addEventListener(‘fetch’, event => {

async function run(event) {

const { request } = event;
const cache = caches.default;
const url = new URL(request.url);
const ua = request.headers.get(‘user-agent’);
let uaValue = ‘desktop’; = ‘’;

if (/Mobile|Android|Silk|Kindle|BlackBerry|Opera Mini|Opera Mobi/i.test(ua)) {
uaValue = ‘mobile’;
url.searchParams.set(‘ua’, uaValue);

const newRequest = new Request(url, request);

let response = await cache.match(newRequest);
if (!response) {
response = await fetch(request, { cf: { cacheEverything: true }});
event.waitUntil(cache.put(newRequest, response.clone()));

return response;