Overview
The Shared module (shared.js) provides common utilities used across all components. It handles Progressive Web App (PWA) installation prompts, service worker registration for offline functionality, and utility functions for formatting and user notifications.
Responsibilities
- Capture and handle PWA install prompts
- Register service worker for offline capability
- Format monetary values consistently
- Display toast notifications to users
PWA Installation
beforeinstallprompt Event Handler
Captures the browser’s PWA install prompt and shows a custom install button.- Prevent Default: Prevents automatic browser install prompt
- Store Prompt: Saves the install prompt event in
deferredPrompt - Show Button: Reveals the
#installBtnelement by removinghiddenclass - Click Handler: Attaches event listener to install button
- Trigger Prompt: When clicked, shows the native install dialog
- Wait for Choice: Awaits user’s decision (install or cancel)
- Cleanup: Clears
deferredPromptand hides button after user decides
#installBtn- Button element for triggering PWA install
- App meets PWA criteria (manifest, service worker, HTTPS)
- User hasn’t already installed the app
- Browser supports PWA installation (Chrome, Edge, Samsung Internet, etc.)
Service Worker Registration
Service Worker Setup
Registers the service worker for offline functionality and caching.- Feature Detection: Checks if browser supports service workers
- Wait for Load: Defers registration until page fully loads
- Register Worker: Registers
sw.jswith root scope - Error Handling: Logs registration errors to console
- Path:
./sw.js(relative to HTML page) - Scope:
./(controls all pages in the app)
Utility Functions
money(n)
Formats a number as a currency string with 2 decimal places.The number to format as currency
string - Formatted number with exactly 2 decimal places
- Converts input to number with
Number(n) - Multiplies by 100 to shift decimals
- Rounds to nearest integer with
Math.round() - Divides by 100 to restore decimal position
- Formats to exactly 2 decimal places with
toFixed(2)
showToast(message, type)
Displays a temporary notification toast to the user.The message text to display in the toast
The type of toast, affecting background color
- Create Element: Dynamically creates a
<div>element - Style Toast: Applies Tailwind CSS classes for positioning and styling
- Color Based on Type:
success→ Green background (bg-green-600)error→ Red background (bg-red-600)
- Set Message: Inserts message as text content
- Show Toast: Appends to document body
- Auto-Remove: Removes toast after 3 seconds
- Position: Fixed, bottom-right corner
- Offset: 1rem (16px) from bottom and right edges
- Padding: 1.5rem horizontal, 0.75rem vertical
- Border radius: 0.5rem (8px)
- Text: White, medium weight
- Shadow: Large shadow for depth
- Z-index: 50 (appears above most content)
- POS: Cart operations, checkout confirmation
- Products: Add/delete product feedback
- Dashboard: General notifications
Module Exports
The shared module exports two utility functions:Browser Compatibility
Service Worker
Supported:- Chrome 40+
- Firefox 44+
- Safari 11.1+
- Edge 17+
- Internet Explorer (any version)
- Older mobile browsers
PWA Install Prompt
Supported:- Chrome 68+ (Android)
- Chrome 73+ (Desktop)
- Edge 79+
- Samsung Internet 4+
- Safari (uses different install mechanism)
- Firefox (no native prompt support)
Security Considerations
HTTPS Requirement
Service workers and PWA features require HTTPS (or localhost for development):Service Worker Scope
The service worker registered withscope: './' can only control:
- Pages in the same directory
- Pages in subdirectories
- Cannot control parent directories
Initialization Order
The shared module sets up global functionality immediately on import:- PWA Prompt: Listens for
beforeinstallpromptevent (fires when browser determines app is installable) - Service Worker: Registers on
window.loadevent (after page resources load) - Exports: Makes
money()andshowToast()available to other modules