[英]Fetch() request immediately failing in service worker running through Android Webview
I have a service worker that I use to enable an offline version of my website.我有一个服务工作者,我用它来启用我网站的离线版本。 This works great.
这很好用。 I also have an Android app that is basically just a wrapper around a webview that loads my website.
我还有一个 Android 应用程序,它基本上只是加载我的网站的 web 视图的包装器。
All was fine and dandy until about 2-3 weeks ago when the Fetch() request started immediately failing.一切都很好,直到大约 2-3 周前 Fetch() 请求立即开始失败。 It is only failing when running through the Android webview.
只有在通过 Android webview 运行时才会失败。 Running through a browser works fine.
通过浏览器运行工作正常。 If the resource is cached already (ie via the install event) then it works great, it's only when I get a page that is not cached.
如果资源已经缓存(即通过安装事件),那么它工作得很好,只有当我得到一个没有缓存的页面时。
The code in my service worker:我的服务工作者中的代码:
self.addEventListener("fetch", function (event) {
if (event.request.method !== 'GET'
|| event.request.url.toLowerCase().indexOf('//ws') > -1
|| event.request.url.toLowerCase().indexOf('localws') > -1) {
// Don't intercept requests made to the web service
// If we don't block the event as shown below, then the request will go to
// the network as usual.
return;
}
event.respondWith(async function () {
// override the default behavior
var oCache = await caches.open('cp_' + version);
var cached = await oCache.match(event.request.url);
if (cached && cached.status < 300) {
return cached;
}
// Need to make a call to the network
try {
var oResp = await fetch(event.request); // THIS LINE CAUSES THE PROBLEM!!!
return oResp;
} catch (oError) {
console.log('SW WORKER: fetch request to network failed.', event.request);
return new Response('<h1>Offline_sw.js: An error has occured. Please try again.</h1><br><h2>Could not load URL: ' + event.request.url + '</h2>', {
status: 503,
statusText: 'Service Unavailable',
headers: new Headers({
'Content-Type': 'text/html'
})
});
}
}()); // event.respondwith
}); // fetch
The line:线路:
var oResp = await fetch(event.request);
var oResp = await fetch(event.request);
is called once I've determined it is not cached and seems to be the culprit.一旦我确定它没有被缓存并且似乎是罪魁祸首,就会被调用。 When it errors out I get the following error in my catch(): 'Failed to fetch'
当它出错时,我在 catch() 中收到以下错误:“无法获取”
This seems pretty generic and not helpful.这看起来很一般,没有帮助。 Again, this works when going through a browser and so I know it's not a CORS issue, service worker in the wrong directory, etc. Again, it worked until about 3 weeks ago and now I'm getting reports from customers that it's not working.
同样,这在通过浏览器时有效,所以我知道这不是 CORS 问题,不是服务人员在错误的目录中等。同样,它一直工作到大约 3 周前,现在我从客户那里收到报告说它不起作用.
Here's a screen shot of the actual event.request that I'm sending off:这是我发送的实际 event.request 的屏幕截图:
In the chrome developer tools (used to debug the webview) I see the following:在 chrome 开发人员工具(用于调试 webview)中,我看到以下内容:
Am I doing something wrong or is this a bug in the webview / chrome that was released recently?我做错了什么还是这是最近发布的 webview/chrome 中的错误? (I say that as chrome powers the webview)
(我这么说是因为 chrome 为 webview 提供了动力)
Looks like it was a bug in chromium.看起来这是铬的错误。 See bugs.chromium.org/p/chromium/issues/detail?id=977784.
请参阅 bugs.chromium.org/p/chromium/issues/detail?id=977784。 Should be fixed in v 76.
应该在 v 76 中修复。
As a work around (as the link mentioned), you can add the following to your android code:作为解决方法(如链接所述),您可以将以下内容添加到您的 android 代码中:
ServiceWorkerController oSWController = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
oSWController = ServiceWorkerController.getInstance();
oSWController.setServiceWorkerClient(new ServiceWorkerClient(){
@Nullable
@Override
public WebResourceResponse shouldInterceptRequest(WebResourceRequest request){
return super.shouldInterceptRequest(request);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.