【发布时间】:2018-08-04 05:06:39
【问题描述】:
ServiceWorker 是否有任何方法可以识别 no-cors/opaque FetchEvent.Request 的来源或来源?或者我们是否可以将识别信息/数据从 HTML 显式传递到 ServiceWorker,以用于识别 FetchEvents 的来源?我使用查询字符串参数编写了一个解决方案,但我正在寻找比这更原生或更优雅的东西:
HTML
<img id="img1" src="image.jpg?imgId=img1" />
<img id="img2" src="image.jpg?imgId=img2" />
服务工作者
self.addEventListener('fetch', event => {
const url = new URL(event.request.url);
if(url.pathname.endsWith('/image.jpg')) {
// get Element ID that initated the FetchEvent
const imgId = url.searchParams.get('imgId');
// Notify document that the fetch started
clients.get(event.clientId)
.then(client => client.postMessage({imgId}));
}
})
【问题讨论】:
标签: service-worker fetch-api service-worker-events