防止重新加载的一种方法是在您的 Web 应用程序清单中设置一个虚假路径,该路径仅用于接收共享(请注意,这使用 HTTP POST 和“multipart/form-data”编码 [所以稍后您可以扩展应用程序以接收整个文件]):
{
"share_target": {
"action": "/receive-shares",
"method": "POST",
"enctype": "multipart/form-data",
"params": {
"title": "name",
"text": "description",
"url": "link"
}
}
}
然后在你的 service worker 的 fetch 处理程序中,你处理传入的共享并将用户重定向到 home:
self.addEventListener('fetch', (e) => {
if ((e.request.url.endsWith('/receive-shares')) &&
(e.request.method === 'POST')) {
return e.respondWith((async () => {
// This function is async.
const formData = await fetchEvent.request.formData();
// Do something with the URL…
const url = formData.get('url');
// Store the URL, process it, communicate it to the clients…
// You need to redirect the user somewhere, since the path
// /receive-shares does not actually exist.
return Response.redirect('/', 303);
})())
}
/* Your regular fetch handler */
})