Initial commit with 2026 World Cup Quant Platform core modules and CI/CD
This commit is contained in:
60
platform/web/components/PwaBootstrap.tsx
Normal file
60
platform/web/components/PwaBootstrap.tsx
Normal file
@@ -0,0 +1,60 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect } from 'react';
|
||||
|
||||
function toUint8Array(base64: string): Uint8Array {
|
||||
const padding = '='.repeat((4 - (base64.length % 4)) % 4);
|
||||
const base64Safe = (base64 + padding).replace(/-/g, '+').replace(/_/g, '/');
|
||||
const raw = atob(base64Safe);
|
||||
const buffer = new ArrayBuffer(raw.length);
|
||||
const out = new Uint8Array(buffer);
|
||||
|
||||
for (let i = 0; i < raw.length; i += 1) {
|
||||
out[i] = raw.charCodeAt(i);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
export function PwaBootstrap() {
|
||||
useEffect(() => {
|
||||
if (typeof window === 'undefined' || !('serviceWorker' in navigator)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const setup = async () => {
|
||||
try {
|
||||
const registration = await navigator.serviceWorker.register('/sw.js');
|
||||
await registration.update();
|
||||
|
||||
if (Notification.permission === 'granted') {
|
||||
const vapid = process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY;
|
||||
if (vapid) {
|
||||
const existing = await registration.pushManager.getSubscription();
|
||||
if (!existing) {
|
||||
await registration.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: toUint8Array(vapid),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
navigator.serviceWorker.addEventListener('message', (event) => {
|
||||
const payload = event.data;
|
||||
if (payload?.type === 'push-trigger') {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('PWA push message', payload);
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn('PWA bootstrap 失敗', error);
|
||||
}
|
||||
};
|
||||
|
||||
void setup();
|
||||
}, []);
|
||||
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user