【发布时间】:2020-07-02 17:07:36
【问题描述】:
我正在尝试通过工作箱创建自定义服务工作者来使用 vue.js 的渐进式 Web 应用程序功能。每次我尝试构建应用程序时,都会出现以下错误:
AssertionError [ERR_ASSERTION]: swSrc must be set to the path to an existing service worker file.
项目/vue.config.js:
module.exports = {
runtimeCompiler: true,
pwa: {
workboxPluginMode: "InjectManifest",
plugins: [
new InjectManifest({
swSrc: "src/service-worker.js"
})
]
}
};
project/src/service-worker.js:
self.__precacheManifest = [].concat(self.__precacheManifest || []);
workbox.precaching.suppressWarnings();
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});
//Web Push Notifications//
let click_open_url;
self.addEventListener("push", function(event) {
let push_message = event.data.json();
// push notification can send event.data.json() as well
click_open_url = push_message.notification.data.url;
const options = {
body: push_message.notification.body,
icon: push_message.notification.icon,
image: push_message.notification.image,
tag: "alert"
};
event.waitUntil(
self.registration.showNotification(push_message.notification.title, options)
);
});
self.addEventListener("notificationclick", function(event) {
const clickedNotification = event.notification;
clickedNotification.close();
if (click_open_url) {
const promiseChain = clients.openWindow(click_open_url);
event.waitUntil(promiseChain);
}
});
我尝试将 swSrc 上的格式更改为以 ./ 或仅以 / 开头,甚至删除 src/ 但这些都没有做任何事情。我也尝试过处理workbox生成的代码,然后将其粘贴到service-worker.js中,但它仍然无法识别它。如何让 InjectManifest 识别我的 Service Worker 文件?
【问题讨论】:
标签: javascript vue.js service-worker progressive-web-apps web-notifications