You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Brave禁用Shield时Fetch API跨域请求报错的排查与修复

问题:Brave禁用Shield时Service Worker拦截跨域请求报错

背景

  • 仅在Brave浏览器禁用Shield时触发该问题
  • 博客中使用OWA分析脚本,地址为 https://stats.jcubic.pl/modules/base/js/owa.tracker-combined-min.js
  • 同时自研了Service Worker库Wayne,功能是拦截所有请求并生成纯浏览器端HTTP响应,API设计参考Node.js的Express.js,主要用来给浏览器中打开的JS、CSS文件添加语法高亮

错误信息

Fetch API cannot load https://stats.jcubic.pl/modules/base/js/owa.tracker-combined-min.js. Request mode is "no-cors" but the redirect mode is not "follow".

补充说明:该URL不存在任何重定向,且stats.jcubic.pl域名已启用CORS配置。

相关代码

Service Worker核心拦截逻辑(已剔除无关代码)

self.addEventListener('fetch', (event) => {
    if (filter(event.request) === false) {
        return;
    }
    const promise = new Promise(async (resolve, reject) => {
        const req = event.request;
        try {
            // 来自示例的代码段,不清楚具体作用
            if (event.request.cache === 'only-if-cached' &&
                event.request.mode !== 'same-origin') {
                return;
            }
            
            fetch(event.request).then(resolve).catch(reject);
        } catch(error) {
            this._handle_error(resolve, req, error);
        }
    });
    event.respondWith(promise.catch(() => {}));
});

注:上述代码中if (event.request.cache === 'only-if-cached' && event.request.mode !== 'same-origin')这段逻辑来自某个示例,目前不清楚其作用。

博客中加载OWA的代码片段

<script type="text/javascript">
 //<![CDATA[
 var owa_baseUrl = 'https://stats.jcubic.pl/';
 var owa_cmds = owa_cmds || [];
 owa_cmds.push(['setSiteId', 'b9a1a51b38e3837c9a5b309ec4122ff3']);
 owa_cmds.push(['trackPageView']);
 owa_cmds.push(['trackClicks']);
 (function() {
     var _owa = document.createElement('script'); _owa.type = 'text/javascript'; _owa.async = true;
     owa_baseUrl = ('https:' == document.location.protocol ? window.owa_baseSecUrl || owa_baseUrl.replace(/http:/, 'https:') : owa_baseUrl );
     _owa.src = owa_baseUrl + 'modules/base/js/owa.tracker-combined-min.js';
     var _owa_s = document.getElementsByTagName('script')[0]; _owa_s.parentNode.insertBefore(_owa, _owa_s);
 }());
 //]]>
</script>

已尝试方案及需求

  • 尝试给OWA脚本标签添加_owa.crossorigin = '',无效果
  • 曾考虑给fetch事件添加跳过标识,但Promise回调为异步逻辑,无法实现
  • 核心需求:
    1. 明确该错误产生的根本原因
    2. 解决拦截所有请求时的这个问题,优先考虑在Wayne库本身修复,让所有用户受益

内容的提问来源于stack exchange,提问作者jcubic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 08:52:39