|
1 | 1 | # Service Workers & PWA Documentation |
2 | 2 |
|
3 | | -This document explains how Progressive Web App (PWA) features and service workers are implemented in the AlexJSully Portfolio project. |
| 3 | +This document explains how Progressive Web App (PWA) features are implemented in the AlexJSully Portfolio project using native Next.js capabilities. |
4 | 4 |
|
5 | | -## 📦 Purpose |
| 5 | +## Purpose |
6 | 6 |
|
7 | | -PWA support enables offline access, faster load times, and installable experiences for users. |
| 7 | +PWA support enables: |
8 | 8 |
|
9 | | -## 🏗️ Structure |
| 9 | +- **Installability**: Users can install the website as a standalone app. |
| 10 | +- **Offline Capabilities**: Basic offline support via browser caching and service workers (if configured). |
| 11 | +- **Native-like Experience**: Standalone display mode and custom icons. |
10 | 12 |
|
11 | | -## 🔍 Usage Example |
| 13 | +## Architecture |
12 | 14 |
|
13 | | -## 🚀 Features |
| 15 | +The PWA implementation relies on Next.js's built-in metadata and route handlers, specifically `manifest.ts`. |
14 | 16 |
|
15 | | -- **Offline caching:** Assets, pages, and API responses are cached for offline use. |
16 | | -- **Service worker:** Handles background sync, cache updates, and push notifications. |
17 | | -- **Web App Manifest:** Enables installability and controls app appearance on devices. |
18 | | -- **Responsive design:** Optimized for mobile and desktop. |
| 17 | +```mermaid |
| 18 | +flowchart TD |
| 19 | + Browser[User Browser] |
| 20 | + Manifest["src/app/manifest.ts"] |
| 21 | + Icons["public/icon/"] |
| 22 | + NextJS[Next.js Server] |
19 | 23 |
|
20 | | -## ⚙️ Technical Implementation |
| 24 | + Browser -- Requests Manifest --> NextJS |
| 25 | + NextJS -- Generates JSON --> Manifest |
| 26 | + Manifest -- References --> Icons |
| 27 | + Browser -- Downloads --> Icons |
| 28 | + Browser -- Install Prompt --> User |
| 29 | +``` |
21 | 30 |
|
22 | | -### next-pwa Configuration |
| 31 | +## Features |
| 32 | + |
| 33 | +- **Dynamic Manifest**: Generated programmatically via `src/app/manifest.ts`. |
| 34 | +- **Responsive Icons**: Multiple icon sizes and maskable icons for different devices. |
| 35 | +- **Theming**: Custom theme and background colors defined in the manifest. |
| 36 | +- **Display Modes**: Supports `standalone`, `minimal-ui`, and `window-controls-overlay`. |
| 37 | + |
| 38 | +## Technical Implementation |
| 39 | + |
| 40 | +### Manifest Generation (`src/app/manifest.ts`) |
| 41 | + |
| 42 | +Instead of a static `manifest.json`, we use a TypeScript file to generate the manifest dynamically. This allows for type safety and easier maintenance. |
| 43 | + |
| 44 | +```typescript |
| 45 | +// src/app/manifest.ts |
| 46 | +import type { MetadataRoute } from 'next'; |
| 47 | + |
| 48 | +export default function manifest(): MetadataRoute.Manifest { |
| 49 | + return { |
| 50 | + name: "Alexander Sullivan's Portfolio", |
| 51 | + short_name: "Alexander Sullivan's Portfolio", |
| 52 | + // ... other properties |
| 53 | + icons: [ |
| 54 | + { |
| 55 | + src: '/icon/android-chrome-192x192.png', |
| 56 | + sizes: '192x192', |
| 57 | + type: 'image/png', |
| 58 | + }, |
| 59 | + // ... other icons |
| 60 | + ], |
| 61 | + }; |
| 62 | +} |
| 63 | +``` |
23 | 64 |
|
24 | | -- The `next-pwa` plugin is configured in `next.config.js`: |
25 | | - - Specifies service worker location, caching strategies, and runtime behaviors. |
26 | | - - Example config: |
| 65 | +### Service Worker Lifecycle |
27 | 66 |
|
28 | | -```js |
29 | | -// next.config.js |
30 | | -const withPWA = require('next-pwa'); |
31 | | -module.exports = withPWA({ |
32 | | - pwa: { |
33 | | - dest: 'public', |
34 | | - register: true, |
35 | | - skipWaiting: true, |
36 | | - disable: process.env.NODE_ENV === 'development', |
37 | | - }, |
38 | | -}); |
39 | | -``` |
| 67 | +While `next-pwa` previously handled service worker generation, native Next.js apps can use manual service worker registration or rely on standard browser caching headers. |
40 | 68 |
|
41 | | -### Manifest & Icons |
| 69 | +```mermaid |
| 70 | +sequenceDiagram |
| 71 | + participant User |
| 72 | + participant Browser |
| 73 | + participant Network |
| 74 | + participant Cache |
42 | 75 |
|
43 | | -- `public/manifest.json` defines app name, icons, theme color, and display mode. |
44 | | -- Icons for various devices are in `public/icon/`. |
| 76 | + User->>Browser: Visits Site |
| 77 | + Browser->>Network: Request Resources |
| 78 | + Network-->>Browser: Return Resources |
| 79 | + Browser->>Cache: Cache Static Assets (via Headers) |
45 | 80 |
|
46 | | -### Integration Flow |
| 81 | + opt Offline Mode |
| 82 | + User->>Browser: Visits Site (Offline) |
| 83 | + Browser->>Cache: Check Cache |
| 84 | + Cache-->>Browser: Return Cached Assets |
| 85 | + end |
| 86 | +``` |
47 | 87 |
|
48 | | -1. User visits site; service worker is registered. |
49 | | -2. Assets and pages are cached according to config. |
50 | | -3. Manifest enables "Add to Home Screen" prompt. |
51 | | -4. Updates are pushed via service worker when available. |
| 88 | +## Customization |
52 | 89 |
|
53 | | -## 🛠️ Customization & Extensibility |
| 90 | +To modify PWA settings: |
54 | 91 |
|
55 | | -- Modify caching strategies in `next.config.js` for custom needs. |
56 | | -- Add push notification support via service worker. |
57 | | -- Update manifest for branding and install experience. |
| 92 | +1. **Manifest**: Edit `src/app/manifest.ts` to change app name, colors, or icons. |
| 93 | +2. **Icons**: Add or replace images in `public/icon/` and update the manifest accordingly. |
58 | 94 |
|
59 | | -## 🔗 Related Docs |
| 95 | +## Related Docs |
60 | 96 |
|
61 | 97 | - [Architecture Overview](./index.md) |
62 | | -- [Usage Guides](../usage/index.md) |
| 98 | +- [Next.js Manifest Documentation](https://nextjs.org/docs/app/api-reference/file-conventions/metadata/manifest) |
0 commit comments