-
Notifications
You must be signed in to change notification settings - Fork 262
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
base: latest
Are you sure you want to change the base?
Conversation
| ); | ||
| const resources = [...scriptSrcs, ...linkHrefs] | ||
| .filter(url => url.startsWith('/') || url.startsWith(self.location.origin)) | ||
| .map(url => new URL(url, self.location.origin).href); |
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.
Could this have been achieved using the queryselector api rather than regex? You could parse the doc as html using https://developer.mozilla.org/en-US/docs/Web/API/DOMParser
Then found the link element using query selector
Might be a bit less error prone/cleaner?
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.
Will try
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.
Tried to use DOMParser , but it is incompatible with service worker as it is a browser DOM API available in window contexts only and it was throwing error
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 here, some of my questions likely stem from misunderstandings as this seems pretty complicated to me! Please DM me or we can do a call if any of my queries are unclear
…nstall event changes
Resolves JIRA:
PArent ticket : 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
Testing:
It’s better to test in the preview environment, as localhost testing won’t provide accurate results.
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/navigation so caching starts, the PWA_STATUS becomes true, and the app is identified as running in standalone mode.)
Turn off Wi-Fi and then reload the page or navigate to any page to verify that the offline page is displayed.
Note this offline page will be only seen for installed pWA app not for canonical sites
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:
Right now , if we are getting launched directly to homepage rendered in Express(i.e -/mundo ), then that page doesn't render offline page when user goes offline.
Sometimes navigating to express rendered pages (eg :any topics page) , wont show offline page
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