|
| 1 | +### Module 10.2: Progressive Web Apps (PWAs) |
| 2 | + |
| 3 | +Progressive Web Apps (PWAs) are a modern approach to building web applications that provide a native app-like experience while retaining the reach and accessibility of the web. They are designed to work offline, load quickly, and deliver a seamless user experience across different devices and platforms. |
| 4 | + |
| 5 | +In this module, we'll delve into the concept of Progressive Web Apps and explore how to create them. We'll also provide examples to illustrate key principles and features of PWAs. |
| 6 | + |
| 7 | +#### Key Concepts: |
| 8 | +1. **Progressive Enhancement:** PWAs are built using progressive enhancement principles, which means they work for everyone, regardless of their browser or device. |
| 9 | + |
| 10 | +2. **Service Workers:** Service workers are a fundamental part of PWAs. They are JavaScript files that run in the background and enable features like offline access, push notifications, and more. |
| 11 | + |
| 12 | +3. **App Shell:** PWAs often use an app shell architecture, which separates the core structure (HTML/CSS) from the dynamic content. This enables faster initial loading. |
| 13 | + |
| 14 | +4. **Web Manifest:** A web app manifest is a JSON file that provides metadata about the web application, such as its name, icons, and colors. |
| 15 | + |
| 16 | +#### Example: Creating a Simple PWA |
| 17 | + |
| 18 | +In this example, we'll create a basic PWA that displays a list of tasks. Users can add, remove, and complete tasks. We'll use HTML, CSS, and JavaScript to build the app and introduce PWA features. |
| 19 | + |
| 20 | +1. **HTML and CSS:** |
| 21 | + |
| 22 | + Create an HTML file for the app structure and a CSS file for styling. The following is a simplified structure: |
| 23 | + |
| 24 | + ```html |
| 25 | + <!-- index.html --> |
| 26 | + <!DOCTYPE html> |
| 27 | + <html> |
| 28 | + <head> |
| 29 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 30 | + <link rel="stylesheet" href="styles.css"> |
| 31 | + <link rel="manifest" href="manifest.json"> |
| 32 | + </head> |
| 33 | + <body> |
| 34 | + <h1>Task List</h1> |
| 35 | + <ul id="tasks"> |
| 36 | + <!-- Tasks will be added here --> |
| 37 | + </ul> |
| 38 | + <button id="addTask">Add Task</button> |
| 39 | + <script src="app.js"></script> |
| 40 | + </body> |
| 41 | + </html> |
| 42 | + ``` |
| 43 | + |
| 44 | + ```css |
| 45 | + /* styles.css */ |
| 46 | + body { |
| 47 | + font-family: Arial, sans-serif; |
| 48 | + margin: 0; |
| 49 | + padding: 0; |
| 50 | + } |
| 51 | + /* Add more CSS for styling */ |
| 52 | + ``` |
| 53 | + |
| 54 | +2. **JavaScript:** |
| 55 | + |
| 56 | + Write JavaScript to manage tasks and add PWA features. This example focuses on the PWA aspects: |
| 57 | + |
| 58 | + ```javascript |
| 59 | + // app.js |
| 60 | + if ('serviceWorker' in navigator) { |
| 61 | + navigator.serviceWorker.register('service-worker.js') |
| 62 | + .then(registration => { |
| 63 | + console.log('Service Worker registered with scope:', registration.scope); |
| 64 | + }) |
| 65 | + .catch(error => { |
| 66 | + console.error('Service Worker registration failed:', error); |
| 67 | + }); |
| 68 | + } |
| 69 | + ``` |
| 70 | + |
| 71 | +3. **Service Worker:** |
| 72 | + |
| 73 | + Create a service worker file (`service-worker.js`) that caches app assets for offline use and serves them: |
| 74 | + |
| 75 | + ```javascript |
| 76 | + // service-worker.js |
| 77 | + const cacheName = 'task-list-v1'; |
| 78 | + const filesToCache = [ |
| 79 | + '/', |
| 80 | + '/index.html', |
| 81 | + '/styles.css', |
| 82 | + '/app.js', |
| 83 | + '/manifest.json', |
| 84 | + ]; |
| 85 | + |
| 86 | + self.addEventListener('install', (e) => { |
| 87 | + e.waitUntil( |
| 88 | + caches.open(cacheName).then((cache) => { |
| 89 | + return cache.addAll(filesToCache); |
| 90 | + }) |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + self.addEventListener('fetch', (e) => { |
| 95 | + e.respondWith( |
| 96 | + caches.match(e.request).then((response) => { |
| 97 | + return response || fetch(e.request); |
| 98 | + }) |
| 99 | + ); |
| 100 | + }); |
| 101 | + ``` |
| 102 | + |
| 103 | +4. **Web Manifest:** |
| 104 | + |
| 105 | + Create a web app manifest (`manifest.json`) to describe the app's metadata: |
| 106 | + |
| 107 | + ```json |
| 108 | + { |
| 109 | + "name": "Task List", |
| 110 | + "short_name": "Tasks", |
| 111 | + "start_url": "/index.html", |
| 112 | + "display": "standalone", |
| 113 | + "background_color": "#fff", |
| 114 | + "theme_color": "#0078e7", |
| 115 | + "icons": [ |
| 116 | + { |
| 117 | + "src": "icon.png", |
| 118 | + "sizes": "192x192", |
| 119 | + "type": "image/png" |
| 120 | + } |
| 121 | + ] |
| 122 | + } |
| 123 | + ``` |
| 124 | + |
| 125 | +5. **Testing:** |
| 126 | + |
| 127 | + Serve the app using a local server or deploy it to a hosting platform. Access the app in a supported browser. You can add the app to your home screen, and it should work offline after the initial visit. |
| 128 | + |
| 129 | +This example illustrates a simplified PWA that caches essential assets for offline use. PWAs can offer much more, such as push notifications, background synchronization, and responsive design. Building a PWA provides a better user experience, improves performance, and increases engagement. |
| 130 | + |
| 131 | +Remember that to fully leverage PWA features, you may need to consider more advanced topics like service |
0 commit comments