改变什么来防止来自服务人员的双重请求? [英] What to change to prevent double request from service worker?
问题描述
请不要标记为重复.这与 SO 上的其他类似问题并不完全相同.它更具体且可完全重现.
- 克隆 <小时>
或者,如果您想手动操作,请按照以下说明操作:
yarn create-next-app app_name
cd app_name &&纱线
- 在 public 文件夹中,创建名为
service-worker.js
的文件并粘贴以下代码:
addEventListener("install", (event) => {self.skipWaiting();console.log("Service Worker 已安装!");});self.addEventListener("fetch", (event) => {event.respondWith((异步函数(){常量 promiseChain = fetch(event.request.clone());//克隆没有区别event.waitUntil(promiseChain);//没有区别返回承诺链;})());});
- 打开
pages/index.js
并在import Head from "next/head";
下方粘贴以下代码:
if (typeof window !== "undefined" && "serviceWorker" in navigator) {window.addEventListener("加载", function () {//可能需要检查一下 sw 是否已经注册navigator.serviceWorker.register("/service-worker.js", { scope: "/" }).then(函数(注册){console.log("软件注册:", 注册);}).catch(函数(注册错误){console.log("软件注册失败:", registrationError);});});}
纱线开发
- 转到
localhost:3000
并确保 Service Worker 已在 (F12)Applications/Service Worker 下加载 - 转到网络"标签并刷新页面几次.查看 service worker 如何为每个请求发送两个请求
<小时>
我需要在
service-worker.js
代码中进行哪些更改,以免出现双重请求?解决方案这就是 Chrome DevTools 显示请求和预期的方式.
有一个从客户端 JavaScript 到 Service Worker 的资源请求和一个从 Service Worker 到服务器的请求.除非服务工作者缓存了响应并且不需要检查服务器是否有更新,否则这种情况总是会发生.
Please do not mark as duplicate. This is not an exact duplicate of the other similar questions here on SO. It's more specific and fully reproducible.
- Clone this repo.
yarn && yarn dev
- Go to
localhost:3000
and make sure under (F12)->Applications->Service workers, the service worker is installed. - Go to Network tab and refresh a few times(F5)
- Observe how the network requests are doubled.
Example of what I see:
Or if you want to do it manually follow the instructions below:
yarn create-next-app app_name
cd app_name && yarn
- in public folder, create file called
service-worker.js
and paste the following code:
addEventListener("install", (event) => { self.skipWaiting(); console.log("Service worker installed!"); }); self.addEventListener("fetch", (event) => { event.respondWith( (async function () { const promiseChain = fetch(event.request.clone()); // clone makes no difference event.waitUntil(promiseChain); // makes no difference return promiseChain; })() ); });
- open
pages/index.js
and just belowimport Head from "next/head";
paste the following code:
if (typeof window !== "undefined" && "serviceWorker" in navigator) { window.addEventListener("load", function () { // there probably needs to be some check if sw is already registered navigator.serviceWorker .register("/service-worker.js", { scope: "/" }) .then(function (registration) { console.log("SW registered: ", registration); }) .catch(function (registrationError) { console.log("SW registration failed: ", registrationError); }); }); }
yarn dev
- go to
localhost:3000
and make sure the service worker has been loaded under (F12)Applications/Service Workers - Go to the Network tab and refresh the page a few times. See how the service worker sends two requests for each one
What do I need to change in the
service-worker.js
code so that there are no double requests?解决方案This is how Chrome DevTools shows requests and is expected.
There is a request for a resource from the client JavaScript to the Service Worker and a request from the Service Worker to the server. This will always happen unless the service worker has the response cached and does not need to check the server for an update.
这篇关于改变什么来防止来自服务人员的双重请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!