PWA Implementation Guide - Next.js Native
Current Status: Production-Ready PWA with Native Next.js Support ✨
The vertical farming app now uses Next.js's built-in PWA support instead of third-party libraries, providing better performance, more control, and seamless integration with Next.js features.
What's Implemented
1. Native Next.js PWA Configuration (next.config.ts
)
- ✅ Custom service worker handling with proper caching headers
- ✅ Manual service worker registration for maximum control
- ✅ Optimized webpack configuration for PWA assets
- ✅ No third-party dependencies required
2. Custom Service Worker (public/sw.js
)
- ✅ Intelligent Caching Strategies:
- API requests: Network First with cache fallback
- Static assets: Cache First with background updates
- Homepage: Stale While Revalidate
- Other pages: Network First with offline fallback
- ✅ Push Notification Support: Full notification handling with actions
- ✅ Background Sync: Offline data synchronization capabilities
- ✅ Offline Support: Graceful degradation when network unavailable
3. Push Notification System (lib/actions/push-notifications.ts
)
- ✅ Server Actions: VAPID-based push notification sending
- ✅ Farm-Specific Alerts: Critical, warning, and info notifications
- ✅ Broadcast Notifications: Send to all subscribed users
- ✅ Subscription Management: Subscribe/unsubscribe functionality
4. Notification Manager Component (components/NotificationManager.tsx
)
- ✅ Permission Handling: Request and manage notification permissions
- ✅ Subscription UI: User-friendly enable/disable interface
- ✅ Status Indicators: Visual feedback for notification state
- ✅ Development Mode: Only shows in development for testing
5. Enhanced Components
- ✅ Install Prompt: Detects and prompts for app installation
- ✅ PWA Status: Development indicators for PWA features
- ✅ Offline Page: Beautiful offline experience with cached data info
6. Existing PWA Elements
- ✅ Web App Manifest (
public/manifest.json
) - ✅ Mobile-optimized meta tags
- ✅ App icons and theme colors
- ✅ Service worker registration
Migration from next-pwa
What Changed:
- ❌ Removed:
next-pwa
dependency - ❌ Removed: Automated service worker generation
- ✅ Added: Custom service worker with advanced features
- ✅ Added: Push notification system with server actions
- ✅ Added: Manual control over caching strategies
- ✅ Added: Background sync capabilities
Benefits of Migration:
- 🚀 Better Performance: No unnecessary abstraction layers
- 🎛️ More Control: Custom caching strategies for farm data
- 📱 Advanced Features: Push notifications and background sync
- 🔧 Easier Debugging: Direct access to service worker code
- 📦 Smaller Bundle: No third-party PWA library overhead
How to Test
Development Mode
npm run dev
Production Testing
npm run build && npm run start
DevTools Inspection
- Open DevTools > Application > Service Workers
- Check Application > Cache Storage for cached resources
- Use Application > Storage to simulate offline mode
- Check Console for service worker logs
Environment Variables
Add these to your .env.local
for push notifications:
# Generate VAPID keys using: npx web-push generate-vapid-keys
VAPID_PUBLIC_KEY=your_public_key_here
VAPID_PRIVATE_KEY=your_private_key_here
Caching Strategies
API Requests (/api/*
)
- Strategy: Network First with cache fallback
- Purpose: Always try for fresh data, fall back to cache if offline
- Cache Duration: 5 minutes for successful responses
Static Assets (.js
, .css
, images)
- Strategy: Cache First with background updates
- Purpose: Fastest loading, update in background
- Cache Duration: Until cache version changes
Homepage (/
)
- Strategy: Stale While Revalidate
- Purpose: Instant loading with background updates
- Cache Duration: Always serve cached, update silently
Other Pages
- Strategy: Network First with offline page fallback
- Purpose: Try network, fall back to cache or offline page
- Fallback: Custom offline page with farm-specific messaging
Push Notification Features
Farm Alert Types
- 🚨 Critical Alerts: Temperature extremes, system failures
- ⚠️ Warning Alerts: Threshold violations, maintenance needed
- 📊 Info Updates: Growth milestones, harvest ready notifications
Notification Actions
- View Details: Opens relevant page in app
- Dismiss: Closes notification
- Background: Clicks focus existing app or open new window
Subscription Management
// Subscribe to notifications
const result = await subscribeToPushNotifications(subscription, userId);
// Send farm alert
await sendFarmAlert('critical', 'Temperature too high in Zone A!', {
sensorId: 'temp_zone_a',
value: 35,
threshold: 30,
url: '/sensors/temp_zone_a'
});
Background Sync Features
Offline Data Sync
- Sensor Data: Queued for sync when connection returns
- Alert Acknowledgments: Saved locally, synced when online
- Settings Changes: Applied locally, synced to server
Sync Events
sensor-data-sync
: Synchronize offline sensor readingsalert-sync
: Sync alert acknowledgments and responses
Offline Experience
What Works Offline:
- ✅ Cached Pages: Previously visited pages load instantly
- ✅ Static Assets: All CSS, JS, images load from cache
- ✅ API Cache: Recent API responses available
- ✅ Offline Page: Beautiful fallback with farm-specific messaging
What Requires Network:
- ❌ Real-time Updates: Live sensor data, real-time alerts
- ❌ New API Calls: Fresh data requests
- ❌ Push Notifications: Sending notifications (receiving works)
File Structure
frontend/
├── src/
│ ├── components/
│ │ ├── InstallPrompt.tsx # App installation UI
│ │ ├── PWAStatus.tsx # Development status
│ │ └── NotificationManager.tsx # Push notification UI
│ ├── lib/
│ │ └── actions/
│ │ └── push-notifications.ts # Server actions for push
│ └── app/
│ ├── layout.tsx # PWA components + SW registration
│ └── offline/
│ └── page.tsx # Offline fallback page
├── public/
│ ├── sw.js # Custom service worker
│ └── manifest.json # Web app manifest
├── next.config.ts # Native PWA configuration
└── docs/
└── PWA_IMPLEMENTATION.md # This documentation
Commands
# Install dependencies (web-push already included)
npm install
# Build with PWA enabled
npm run build
# Test PWA in production mode
npm run start
# Generate VAPID keys for push notifications
npx web-push generate-vapid-keys
# Check service worker in DevTools
# Open DevTools > Application > Service Workers
Advanced Features
Custom Caching for Farm Data
// Add to sw.js for specific farm data caching
{
urlPattern: /\/api\/sensors\/.*/,
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'sensor-data',
expiration: {
maxEntries: 100,
maxAgeSeconds: 300, // 5 minutes
},
},
}
Farm-Specific Push Notifications
// Send critical farm alerts
await sendFarmAlert('critical', 'Water system failure in Greenhouse 2', {
sensorId: 'water_gh2',
url: '/alerts/water_gh2'
});
IndexedDB Integration (Future)
// Store farm data offline
await storeFarmDataOffline({
sensorId: 'temp_001',
value: 24.5,
timestamp: Date.now(),
synchronized: false
});
Next Steps
Phase 1: Enhanced Offline Storage ✅ Completed
- Custom service worker with intelligent caching
- Offline page with farm-specific messaging
- Background sync preparation
Phase 2: Push Notification Integration ✅ Completed
- VAPID-based push notification system
- Farm alert categorization (critical/warning/info)
- Notification permission management UI
Phase 3: Advanced Offline Features (Next)
- IndexedDB integration for offline farm data
- Offline form submissions with sync
- Conflict resolution for offline/online data
Phase 4: Performance Optimization (Future)
- Selective caching based on farm zones
- Predictive caching for frequently accessed data
- Background data refresh strategies
Benefits for Vertical Farming
Current Benefits
- 🏠 Native App Experience: Install on home screen like native app
- 📱 Mobile Optimized: Perfect for greenhouse tablets and mobile devices
- ⚡ Instant Loading: Cached resources load immediately
- 🔕 Offline Monitoring: View cached sensor data without internet
- 📢 Real-time Alerts: Push notifications for critical farm events
Advanced Benefits
- 🔄 Background Sync: Data syncs automatically when connection returns
- 💾 Offline Actions: Make changes offline, sync when connected
- 🎯 Smart Caching: Farm data cached based on access patterns
- 🚀 Better Performance: Native service worker optimized for farm workflows
This implementation provides a production-ready PWA using Next.js's built-in capabilities, offering superior performance, more control, and advanced features specifically designed for vertical farming operations! 🌱