Restore service worker registration code#4
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request restores the service worker registration code that was previously removed, aiming to enable Progressive Web App functionality for offline capabilities. The changes add a setupServiceWorker() method and call it during app initialization.
Changes:
- Added
setupServiceWorker()method to register the service worker at/sw.js - Integrated service worker registration into the app's initialization flow
- Removed trailing whitespace throughout the file for code cleanup
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| window.addEventListener('load', () => { | ||
| navigator.serviceWorker.register('/sw.js') | ||
| .then((registration) => { | ||
| console.log('SW registered: ', registration); | ||
| }) | ||
| .catch((registrationError) => { | ||
| console.log('SW registration failed: ', registrationError); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
The service worker registration is wrapped in a 'load' event listener, but this method is already called from setup() which runs after DOMContentLoaded (see lines 16-20). This creates a problematic nested event listener pattern.
If setupServiceWorker() is called after the window 'load' event has already fired, the inner event listener will never execute and the service worker will not be registered. This is likely to happen when the app loads quickly.
Consider one of these alternatives:
- Register the service worker immediately without the nested 'load' event listener, since setup() already ensures DOM is ready
- Move the service worker registration to happen directly in the constructor or init() method with a proper 'load' check
| window.addEventListener('load', () => { | |
| navigator.serviceWorker.register('/sw.js') | |
| .then((registration) => { | |
| console.log('SW registered: ', registration); | |
| }) | |
| .catch((registrationError) => { | |
| console.log('SW registration failed: ', registrationError); | |
| }); | |
| }); | |
| navigator.serviceWorker.register('/sw.js') | |
| .then((registration) => { | |
| console.log('SW registered: ', registration); | |
| }) | |
| .catch((registrationError) => { | |
| console.log('SW registration failed: ', registrationError); | |
| }); |
Fixes #3
Restores just the SW registration code from c29d8b1. Still leaves out the other PWA events that were removed in that commit.