Cloudflare工作者:阻止带有参数的URL 302来自磁盘缓存

问题描述

我有一个工作人员,当URL具有特定参数时,我想绕过缓存:

      let wcApiParam = url.searchParams.get('wc-api');

      //bypass cache if wc-api param is in the URL
      if( wcApiParam != 'auth'  ){
        //force cache
        response = await fetch( newRequest,{ cf: { cacheTtl: 43200 } } );
      } else {
          //bypass cache
          response = await fetch( newRequest,{ cf: { cacheTtl: 0 } } );
      }

我不确定自己在做什么错,请求的响应始终为302 (from disk cache),这在我的网站中造成了错误,因为我需要避免缓存这些请求。我的工人怎么了?还是我的缓存设置有问题?

下面是完整的答复:

Request response

解决方法

问题似乎是响应已缓存在浏览器的缓存中。该请求实际上并没有达到Cloudflare的要求(因此您的Worker没有执行)。

浏览器已缓存响应,因为它具有Cache-Control: max-age=14400。这告诉浏览器它可以将响应缓存4小时。您应该停止从源头发送此标头,或者可以让Worker脚本从不应缓存在浏览器中的响应中删除标头,例如:

let response = await fetch(...);

// Copy the response so that headers can be modified.
response = new Response(response);

// Remove cache-control header.
response.headers.delete("Cache-Control");