I wrote this code as a Cloudflare Service Worker which is meant to precisely emulate their native function for "Bypass cache on cookie". Specifically, if someone has a Wordpress cookie - it would bypass cache, otherwise it does not.
It does not seem to function at all - in that despite having a cookie and being logged in (confirmed via Chrome developer tools) - I still get a Cloudflare cache HIT on this example domain - Tallyfy. Anything wrong with it? Help appreciated!
// A Service Worker which skips cache if the request contains a cookie. addEventListener('fetch', event => { let request = event.request; var flag=false; if(request.headers.cookie) { var pairs = request.headers.cookie.split(";"); var patt = new RegExp("wp-.*|wordpress.*|comment_.*|woocommerce_.*") for(var i=0;i<pairs.length;i++){ if(patt.test(pairs[i])){ flag = true; break; } } } if (request.headers.has('Cookie') && flag) { // Cookie present. Add Cache-Control: no-cache. let newHeaders = new Headers(request.headers) newHeaders.set('Cache-Control', 'no-cache') event.respondWith(fetch(request, {headers: newHeaders})) } // Use default behavior. return })