英文:
How to add custom service worker to Nuxt 3 with vite-pwa?
问题
I am trying to glue together a Nuxt 3 PWA with vite-plugin-pwa
, more specifically @vite-pwa/nuxt
. I installed the module and set up basic configuration (I basically followed this video)
我正在尝试将Nuxt 3 PWA与 vite-plugin-pwa
组合在一起,更具体地说是 @vite-pwa/nuxt
。我安装了这个模块并设置了基本配置(基本上是按照这个视频进行的)。
What I want
The first feature I would like to implement is sending Push Notifications to users at fixed time. They should receive the Push Notifications even if they are not in the app. That's why I added the PWA module.
我想要实现的第一个功能是在固定时间向用户发送推送通知。即使他们不在应用程序中,他们也应该收到推送通知。这就是为什么我添加了PWA模块的原因。
What I do not understand
I don't know how to add my custom service worker. If I understand it correctly, I could write a service worker, that just sends Push Notifications with the Notification API to my app. No need for services like OneSignal (right?).
我不知道如何添加自定义的service worker。如果我理解正确,我可以编写一个service worker,只需使用Notification API向我的应用程序发送推送通知。不需要像OneSignal这样的服务(对吗?)。
As I understand the documentation Vite PWA uses google's workbox under the hood and per default it generates a service worker. If you set injectRegister: 'script'
it should inject a service worker registration in the head of my app (like described here).
据我理解,Vite PWA文档使用了Google的workbox,并且默认生成了一个service worker。如果您设置injectRegister: 'script'
,它应该会在我的应用程序头部注入一个service worker注册(就像这里描述的那样)。
Now when I search the source code via the developer tools I cannot find any serviceWorker script. Strangely enough, when I go to the application tab in the dev tools, I can see that a service worker dev-sw.js
is registered. How did this get added?
现在,当我通过开发者工具搜索源代码时,我找不到任何serviceWorker脚本。奇怪的是,当我转到开发工具中的应用程序选项卡时,我可以看到注册了一个service worker dev-sw.js
。这是如何添加的?
I think I have to set a mode in the configuration to tell workbox not to generate the service worker registration for me. This mode should be called injectManifest, as described here and here. But again, how do I add this to my code?
我认为我必须在配置中设置一个模式,告诉workbox不要为我生成service worker注册。这个模式应该叫做injectManifest,如这里和这里所述。但是,我该如何将其添加到我的代码中?
As suggested in the documentation I also had a look at the elk repo. But unfortunately they take a different approach and currently do not use vite-pwa/nuxt.
如文档中建议的,我还查看了elk repo。但不幸的是,他们采用了不同的方法,目前不使用vite-pwa/nuxt。
How can I add my custom service worker to send push notifications?
我如何添加自定义的service worker来发送推送通知?
Does anybody have experience with PWAs in Nuxt 3 and specifically working with service workers and sending Push Notifications?
有没有人在Nuxt 3中有PWAs的经验,特别是在与service worker和发送推送通知方面的工作经验?
英文:
I am trying to glue together a Nuxt 3 PWA with vite-plugin-pwa
, more specifically @vite-pwa/nuxt
. I installed the module and set up basic configuration (I basically followed this video)
What I want
The first feature I would like to implement is sending Push Notifications to users at fixed time. They should receive the Push Notifications even if they are not in the app. That's why I added the PWA module.
What I do not understand
I don't know how to add my custom service worker. If I understand it correctly, I could write a service worker, that just sends Push Notifications with the Notification API to my app. No need for services like OneSignal (right?).
As I understand the documentation Vite PWA uses google's workbox under the hood and per default it generates a service worker. If you set injectRegister: 'script'
it should inject a service worker registration in the head of my app (like described here).
Now when I search the source code via the developer tools I cannot find any serviceWorker script. Strangely enough, when I go to the application tab in the dev tools, I can see that a service worker dev-sw.js
is registered. How did this get added?
​
I think I have to set a mode in the configuration to tell workbox not to generate the service worker registration for me. This mode should be called injectManifest, as described here and here. But again, how do I add this to my code?
As suggested in the documentation I also had a look at the elk repo. But unfortunately they take a different approach and currently do not use vite-pwa/nuxt.
How can I add my custom service worker to send push notifications?
Does anybody have experience with PWAs in Nuxt 3 and specifically working with service workers and sending Push Notifications?
答案1
得分: 3
我遇到了同样的问题,并最终通过 vite-pwa/nuxt git 仓库的问题页面 寻求帮助。
vite-pwa
插件本身不处理这个方面,而是根据 vite-pwa 网站上的基本文档 使用底层库。
要注册服务工作者,你需要以下几个步骤:
- 通过 yarn 或 npm 安装
workbox-core
、workbox-precaching
和workbox-routing
作为依赖项。 - 在你的应用程序的
public
目录中创建一个服务工作者的 JavaScript 文件(默认情况下,vite-pwa 寻找sw.js
)。 - 在你的
nuxt.config.ts
文件中设置pwa.strategies
属性为injectManifest
。 - 更新你的服务工作者文件,完成配置。
我建议复制 @vite-pwa/nuxt
默认创建的服务工作者默认模板(这应该类似于你通过检查 dev-sw.js
文件得到的内容,因为我从那里复制了这段代码):
import { clientsClaim } from 'workbox-core';
import { precacheAndRoute, cleanupOutdatedCaches, createHandlerBoundToURL } from 'workbox-precaching';
import { registerRoute, NavigationRoute } from 'workbox-routing';
self.skipWaiting();
clientsClaim();
precacheAndRoute(self.__WB_MANIFEST);
cleanupOutdatedCaches();
// 如果不需要预缓存任何内容,可以删除此代码,或者保留它并忽略警告消息
try {
const handler = createHandlerBoundToURL('/');
const route = new NavigationRoute(handler);
registerRoute(route);
} catch (error) {
console.warn('在注册缓存路由时出现错误', { error });
}
// 在这里添加你的服务工作者代码。
最后,我选择使用 Firebase Cloud Messaging 并使用 Firebase SDK 进行推送通知,遵循 这个指南。我需要一个后端服务来发送推送通知(我使用 asp.net core,但任何支持 Firebase Admin SDK 的东西都应该可以工作),但是你也可以只使用服务工作者,通过配置你的 PWA 后,可以考虑使用 这个答案。这在配置你的 PWA 后应该是可行的。
更明确的编辑:
dev-sw.js
文件是如何添加的
vite-pwa(@vite-pwa/nuxt
用于修改 vite 构建的库)根据你在 nuxt.config.ts
文件中传递给 pwa
属性的配置规则生成 PWA,然后每当浏览器请求 /sw.js
文件时,nuxt 会提供它。
英文:
I ran into the same problem and ended up reaching out via the issues on the vite-pwa/nuxt git repo.
This aspect of vite-pwa
is not handled by the @vite-pwa/nuxt
plugin itself. Instead it uses the underlying library as per the base documentation found on the vite-pwa website.
In order to get the service-worker registered you will need a few things:
- Install the
workbox-core
,workbox-precaching
, andworkbox-routing
as dependencies via either yarn or npm. - Create a service-worker javascript file (by default, vite-pwa looks for
sw.js
) in thepublic
directory of your application. - Set the
pwa.strategies
property in yournuxt.config.ts
file toinjectManifest
. - Update your service-worker file, and you're good.
I would suggest copying the service-worker default template that @vite-pwa/nuxt
creates by default (this should be similar to what you got by inspecting the dev-sw.js
file, as that is where I copied this code from, kinda):
import { clientsClaim } from 'workbox-core'
import { precacheAndRoute, cleanupOutdatedCaches, createHandlerBoundToURL } from 'workbox-precaching';
import { registerRoute, NavigationRoute } from 'workbox-routing';
self.skipWaiting();
clientsClaim();
precacheAndRoute(self.__WB_MANIFEST);
cleanupOutdatedCaches();
//You can remove this code if you aren't precaching anything, or leave it in and live with the warning message
try {
const handler = createHandlerBoundToURL('/');
const route = new NavigationRoute(handler);
registerRoute(route);
} catch (error) {
console.warn('Error while registering cache route', { error });
}
//Your service-worker code here.
I ended up going with Firebase Cloud Messaging using the firebase SDK for push notifications following this guide. I needed a backend service to send your push notifications (I am using asp.net core, but anything that supports the Firebase Admin SDK should work), however you might be able to get away with just using the service-worker, a quick google turned up this answer which should be viable after configuring your PWA.
Edit for more clarity:
How did the dev-sw.js
file get added
vite-pwa (the library that @vite-pwa/nuxt
uses to modify the vite build) generates the PWA based off of the configuration rules you pass into the pwa
property in your nuxt.config.ts
file, this is then served by nuxt whenever the browser requests the /sw.js
file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论