当我尝试在 netlify 上测试我的 Service Worker 时,它没有注册正确的服务工作者。相反,它注册了一个默认的虚拟工作箱服务工作者。
在使用 React.js 开发 Web 应用程序时,我尝试在 netlify 上注册以下 Service Worker:
self.addEventListener('push', function(event) {
event.waitUntil(
event.data.json()
.then(e => {
return self.registration.showNotification(e.title, e.body);
})
);
});
我运行注册码,它似乎注册了一个文件。
export function registerPush() {
return navigator.serviceWorker
.register('./service-worker.js', {scope: '/'} )
.then((registration) => {
console.log('Service worker successfully registered');
return registration;
})
.catch((err) => console.error('Unable to register service worker.', err));
}
我的浏览器中显示的 Service Worker 似乎基本上是无操作的。
/**
* Welcome to your Workbox-powered service worker!
*
* You'll need to register this file in your web app and you should
* disable HTTP caching for this file too.
*
* The rest of the code is auto-generated. Please don't update this file
* directly; instead, make changes to your Workbox build configuration
* and re-run your build process.
*/
importScripts("/workbox-v3.6.3/workbox-sw.js");
workbox.setConfig({modulePathPrefix: "/workbox-v3.6.3"});
importScripts(
"/precache-manifest.9c03d0ff7f2cae1038c31b553a6a799e.js"
);
workbox.clientsClaim();
/**
* The workboxSW.precacheAndRoute() method efficiently caches and responds to
* requests for URLs in the manifest.
*/
self.__precacheManifest = [].concat(self.__precacheManifest || []);
workbox.precaching.suppressWarnings();
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});
workbox.routing.registerNavigationRoute("/index.html", {
blacklist: [/^\/_/,/\/[^/]+\.[^/]+$/],
});
我的意图是将推送通知添加到我的网络应用程序中。有任何想法吗?我已经测试了服务工作人员的相对路径(它是正确的)并验证了代码是否注册了任何其他服务工作人员(它没有)。