This project demonstrates how to implement advanced service worker strategies in a Progressive Web App (PWA), including offline support, background sync, push notifications, and caching strategies.
- Cache-First Strategy: Caches static assets and serves them from the cache first.
- Network-First Strategy: Tries to fetch API data from the network first, and falls back to the cache if the network is unavailable.
- Stale-While-Revalidate Strategy: Serves cached content while fetching the latest version from the network.
- Background Sync: Ensures form data is synced with the server when connectivity is restored.
- Push Notifications: Sends notifications to users even when the app is not open.
index.html
: Main HTML file for the PWA.styles.css
: CSS file for styling the PWA.offline.html
: Offline fallback page.app.js
: JavaScript file for service worker registration and IndexedDB operations.service-worker.js
: Service worker file implementing caching strategies and background sync.server.js
: Node.js server to handle API requests and serve static files.
- Node.js and npm installed on your machine.
-
Clone the repository:
git clone <repository-url> cd <repository-directory>
-
Initialize the project and install dependencies:
npm init -y npm install express body-parser
-
Start the Node.js server:
node server.js
-
Open your browser and navigate to
http://localhost:3000
.
- Service Worker Registration: Registers the service worker and sets up caching strategies.
- Form Submission: Saves form data to IndexedDB and syncs it with the server when connectivity is restored.
- Push Notifications: Displays push notifications sent from the server.
- Console Logs: Check the browser's console for logs and errors.
- IndexedDB: Inspect the
form-data
database in the Application tab of the browser's developer tools. - Network Requests: Monitor network requests in the Network tab of the browser's developer tools.
This project is licensed under the MIT License.