-
Notifications
You must be signed in to change notification settings - Fork 263
WS-1837: Service worker changes to render offline page #13520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…gh into ws-1837-service-worker-changes
| // -------------------- | ||
| // Helper Functions | ||
| // -------------------- | ||
| const loggerEnabled = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: We will disable logging or delete this before merging
Co-authored-by: Andrew Bennett <andrew.bennett07@bbc.co.uk>
Co-authored-by: Andrew Bennett <andrew.bennett07@bbc.co.uk>
andrewscfc
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work everyone 🚀
amoore108
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks for all the efforts on this.
Just wondering if the service worker gets transpiled by the browser like the other JS on the page? Probably not an issue as I imagine the browsers that support SWs also support the syntax used within it here.
Resolves JIRA:
https://bbc.atlassian.net/browse/WS-1464
https://bbc.atlassian.net/browse/WS-1837
Summary
Service worker changes to render offline page only in PWA app.
Code changes
Changes in _app.page.tsx (nextjs) and serviceWorkerComponet index.tsx(express) - to initiate the service worker registration
Changes in SW.js - to accomodate offline page rendering only for PWA installed app
Added 2 custom hooks for service worker registration and to send PWA_STATUS
How the Caching System Works
Step 1: PWA Identification
Step 2: Offline Page Caching (cacheOfflinePageAndResources)
/:service/offlinepageStep 3: Offline Detection (fetchEventHandler)
i. Return cached offline page HTML
ii. Set flag
isPWADeviceOffline=trueisPWADeviceOfflineflag)isPWADeviceOffline=falseStep 4: Track offline page visits
usePWAOfflineTrackingchecks if user has visited the offline page in the pastpwa-offline-page-view) to indicate that offline page has been visited.Testing:
It’s better to test in the preview environment, as localhost testing won’t provide accurate results (or run the built app instead of the dev server)
Launch any page in the installed PWA app.
Reload the page or navigate to another page to ensure caching happens properly.
(The PWA requires an initial reload or navigation for caching to begin, the PWA_STATUS to become true, and for the app to be identified as running in standalone mode.)
Turn off Wi-Fi, then reload the page or navigate to any page to verify that the offline page is displayed.
Some edge cases related to service worker on rendering offline page :
We need a second reload or any navigation request after the PWA app launch to ensure that:
The service worker does not register on the homepage due to a scope issue (which is an exisiting issue). As a result, rendering the offline page from the homepage will not work at the moment. We will create follow-up tickets to address this.(https://bbc.atlassian.net/browse/WS-2004)
Note : As part of latest discussions, we are going to release offline page and service worker functionality with these edge cases
Useful Links
https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers
https://web.dev/articles/offline-cookbook#on-install-not
https://web.dev/articles/service-worker-lifecycle#updates
https://googlechrome.github.io/samples/service-worker/custom-offline-page/index.html