Skip to content

Conversation

@MohitPatel1
Copy link
Member

@MohitPatel1 MohitPatel1 commented Sep 18, 2024

Comprehensive Update on Push Notifications and Service Worker Enhancements

  • Purpose:
    Implement push notifications and service worker updates for the PWA application.
  • Key Changes:
    • Added support for web push notifications using the web-push library.
    • Implemented a service worker to handle push notifications and service worker updates.
    • Introduced a notification toggle component in the settings drawer for user control.
    • Integrated the vite-plugin-pwa for managing service worker and PWA manifest.
    • Configured the service worker to cache assets and handle push notification events.
    • Updated various dependencies, including node-schedule and web-push.
    • Enhanced existing dependencies like antd, react-toastify, and workbox-core.
    • Added new type definitions for node-schedule and web-push.
    • Improved overall user experience by enabling automatic updates when a new version is available.
  • Impact:
    Users will receive push notifications, enhancing engagement, while the application will automatically update, improving overall user experience.

✨ Generated with love by Kaizen ❤️

Original Description # Add Push Notification Functionality
  • **Purpose:
    **
    Implement push notification capabilities in the API server and PWA client.
  • Key Changes:
    • Added node-schedule and web-push dependencies to handle scheduling and sending push notifications.
    • Implemented endpoints for subscribing, unsubscribing, and sending push notifications.
    • Configured the service worker to handle push events and display notifications.
    • Added a UI component in the PWA to manage push notification preferences.
  • **Impact:
    **
    Users will be able to subscribe to push notifications and receive real-time updates from the application.

✨ Generated with love by Kaizen ❤️

Original Description

@kaizen-bot
Copy link

kaizen-bot bot commented Oct 5, 2024

🔍 Code Review Summary

Attention Required: This push has potential issues. 🚨

Overview

  • Total Feedbacks: 1 (Critical: 1, Refinements: 0)
  • Files Affected: 1
  • Code Quality: [█████████████████░░░] 85% (Good)

🚨 Critical Issues

security (1 issues)

1. Hardcoded application server key in service worker.


📁 File: apps/pwa/src/sw.ts
🔍 Reasoning:
The application server key is hardcoded in the service worker, which poses a significant security risk as it can be easily extracted by malicious actors.

💡 Solution:
Store sensitive keys in environment variables and access them securely.

Current Code:

applicationServerKey: 'BLA70jg5Wgi6XD6BAElOfW7YXcQ3l3iFRzyPj5AV5ZuSr_uTugv-9hbgXwfPhuw_JfbDAqn-Fl5nKSvnQpjFV8g',

Suggested Code:

      applicationServerKey: env.VAPID_PUBLIC_KEY,

✨ Generated with love by Kaizen ❤️

Useful Commands
  • Feedback: Share feedback on kaizens performance with !feedback [your message]
  • Ask PR: Reply with !ask-pr [your question]
  • Review: Reply with !review
  • Update Tests: Reply with !unittest to create a PR with test changes

Copy link

@kaizen-bot kaizen-bot bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider implementing the following changes to improve the code.

// Proceed with subscription if not already subscribed
const registration = await navigator.serviceWorker.ready;
const subscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment: Hardcoded application server key in service worker.

Solution: Store sensitive keys in environment variables and access them securely.
Potential Fix:
applicationServerKey: env.VAPID_PUBLIC_KEY,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be trpc routes.

Comment on lines +15 to +22
export function addSubscription(subscription: PushSubscription) {
pushSubscriptions.add(subscription);
}

export function removeSubscription(subscription: PushSubscription) {
pushSubscriptions.delete(subscription);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If multiple instances of backend are running in production, this will lead to same notification being sent to user multiple times.

Comment on lines +34 to +40
// Schedule a notification every 5 seconds
export function scheduleFrequentNotification() {
scheduleJob('*/5 * * * * *', async () => {
console.log('Sending notification');
await sendNotificationToAll('Frequent Update', 'Here is your notification every 5 seconds!');
});
} No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand you needed this for dev, but then have guardrails to run only in isDev

const registration = await navigator.serviceWorker.ready;
const subscription = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: 'BLA70jg5Wgi6XD6BAElOfW7YXcQ3l3iFRzyPj5AV5ZuSr_uTugv-9hbgXwfPhuw_JfbDAqn-Fl5nKSvnQpjFV8g',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please pick this from env

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seperate SW config from vite config. & add comments with examples on impact/importance for specific fields in config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants