Firefox 服务工作者(作为代理)似乎可以工作,但 JS 模块从未运行过



我在玩服务人员。以下代码应代理 JS 文件以修补导入,以便它们符合平台标准(即"./""../""/""http://..."(。

在 Chromium 中效果很好(在 Arch Linux 上为 67.0.3396.79(。并且似乎在 Firefox(Arch 上的 60.0.2(64 位((中同样有效,至少从网络选项卡中,我可以看到所有修补的源代码都在加载,但由于某种原因 JS 模块没有运行。不能console.log等等。不知道如何让火狐来引导应用程序。

我注意到 fetch 标头都是toLowerCase的,但我在这里阅读了它,Mozilla 还指出标头名称在这里不区分大小写。

我还想可能是因为内容长度可能已更改,因此文件没有被完全接收,但我没有看到任何解析错误,并且网络选项卡确实具有正确的内容长度更改,所以我排除了这一点。

const maybeAppendJS = (x) =>
x.endsWith(".js")
? x
: `${x}.js`;

const maybePatchURL = (x) =>
x.match(/(^'@.*'(.)?$)|(^"@.*"(.)?$)/)
? `"/node_modules/${maybeAppendJS(eval(x))}";`
: x;

const maybePatchImport = (x) =>
x.startsWith("import ")
? x.split(/s/).map(maybePatchURL).join(" ")
: x;

async function maybeRewriteImportStatements(event) {
let candidate = event.request.url;
const url = maybeAppendJS(candidate);
const resp = await fetch(url);
if (!resp.headers.get("content-type").startsWith("text")) {
const text = await resp.text();
const newText = text.split(/n/g)
.map(maybePatchImport)
.join("n");
return new Response(newText, {headers: resp.headers});
}
if (resp.headers.get("content-type").startsWith("text/")) {
const location = `${url.substring(0, url.length - 3)}/index.js`;
return new Response(null, {status: 302, headers: {location: location}});
}
console.log("Service worker should never get here");
}
this.addEventListener('fetch', (event) => {
if (event.request.destination === "script" || event.request.referrer.endsWith(".js") || event.request.url.endsWith(".js")) {
event.respondWith(maybeRewriteImportStatements(event));
}
});

此问题已通过每晚升级到Firefox(62.0a1.20180611-1(修复。

最新更新