我有sw.js
,它接收webpush通知。但最近我intalled vite-PWA-plugin,现在我不能添加默认配置通知.
如何配置此vite.config.ts
以添加到生成的serviceWorker.js
webpush实现?vite.config.ts
:
import {defineConfig} from 'vite';
import laravel from 'laravel-vite-plugin';
import react from '@vitejs/plugin-react';
import path from 'path';
import {VitePWA} from "vite-plugin-pwa";
const manifest = {
"theme_color" : "#2B2B2B",
"background_color": "#2B2B2B",
"display" : "standalone",
"scope" : "/",
"start_url" : "/farm",
"name" : "ColorBit",
"short_name" : "Mining",
"description" : "...",
"icons" : [
{
"src" : "icons/icon-192x192.png",
"sizes": "192x192",
"type" : "image/png"
},
// ...
{
"src" : "icons/maskable_icon.png",
"sizes" : "682x682",
"type" : "image/png",
"purpose": "maskable"
}
]
};
const getCache = ({ name, pattern, strategy = "CacheFirst" }: any) => ({
urlPattern: pattern,
handler: strategy,
options: {
cacheName: name,
expiration: {
maxEntries: 500,
maxAgeSeconds: 60 * 60 * 24 * 60 // 2 months
},
cacheableResponse: {
statuses: [0, 200]
}
}
});
export default defineConfig({
plugins: [
laravel({
input : [ 'resources/js/app.tsx',],
refresh: true,
}),
react({
fastRefresh: false
}),
VitePWA({
registerType: 'autoUpdate',
outDir : path.resolve(__dirname, 'public'),
manifest : manifest,
manifestFilename: 'manifest.webmanifest', // Change name for app manifest
injectRegister : false, // I register SW in app.ts, disable auto registration
workbox : {
globDirectory: path.resolve(__dirname, 'public'), // Directory for caching
globPatterns : [
'{build,images,sounds,icons}/**/*.{js,css,html,ico,png,jpg,mp4,svg}'
],
navigateFallback: null, // Say that we don't need to cache index.html
swDest : 'public/serviceWorker.js',
runtimeCaching: [
// Google fonts cache
getCache({
pattern: /^https:\/\/fonts\.googleapis\.com\/.*/i,
name: "google-fonts-cache",
}),
// Google fonts api cache
getCache({
pattern: /^https:\/\/fonts\.gstatic\.com\/.*/i,
name: "gstatic-fonts-cache"
}),
// Dynamic cache for assets in storage folder
getCache({
pattern: /.*storage.*/,
name: "dynamic-images-cache",
}),
]
}
})
],
resolve: {
alias : {
'@' : path.resolve(__dirname, 'resources/js'),
'@hooks' : path.resolve(__dirname, 'resources/js/hooks'),
'@assets' : path.resolve(__dirname, 'resources/js/assets/'),
'@components': path.resolve(__dirname, 'resources/js/components')
},
extensions: ['.js', '.ts', '.tsx', '.jsx'],
},
});
sw.js
中的旧webpush实现:
// ^^^ Activate, Install, Fetch... ^^^
/* Webpush Notifications */
// Receive push notifications
self.addEventListener('push', function (e) {
if (!(
self.Notification &&
self.Notification.permission === 'granted'
)) {
//notifications aren't supported or permission not granted!
return;
}
if (e.data) {
let message = e.data.json();
e.waitUntil(self.registration.showNotification(message.title, {
body: message.body,
icon: message.icon,
actions: message.actions
}));
}
});
// Click and open notification
self.addEventListener('notificationclick', function(event) {
event.notification.close();
if (event.action === 'farm') clients.openWindow("/farm");
else if (event.action === 'home') clients.openWindow("/");
else if (event.action === 'training') clients.openWindow("/mining-training");
else if (event.action === 'dns') clients.openWindow("/shops/dns");
else if (event.action === 'ali') clients.openWindow("/shops/aliexpress");
else clients.openWindow("/farm");
}, false);
2条答案
按热度按时间relj7zay1#
应该使用inject manifest parametr并通过工作盒准备方法编写自定义serviceWorker(工作盒文档非常糟糕,我认为是这样。你可以使用我的配置中的一些方法)
vite.config.ts
:/resouces/js/serviceWorker.js
:vite-pwa-plugin只有一些关于创建webpush - documentation的信息
我在这个repo中找到了一些service-worker的代码,并从vite.config.ts config生成的旧默认值中复制了一些代码
sauutmhj2#
今天必须用Firebase通知来做这件事。我是这样做的,以防将来这能帮到别人
创建了服务工人文件(包括完整版本,因为我发现很难找到有关如何添加通知的信息)
在我的(React)应用程序中,我注册了这样的前台通知
并在我的App.tsx文件中这样调用它。现在我们得到一个请求通知权限的提示,如果用户接受,我们可以在控制台中看到用户令牌
在我的
vite.config.js
中,在从defineConfig()
返回的对象的顶层,我所要做的就是像这样导入脚本这最后一点花了我最长的时间来弄清楚…希望它能帮助到别人