Introduce Offline Mode #36
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Video demo: https://www.youtube.com/watch?v=-HwAqwH2qq4
What is this?
Offline Modeintroduces a new option to the Settings pane that provides full, reliable caching support for content flowing through the React HN app. It uses the Service Worker Cache API as a backing store and conditionally switches to the Firebase REST API if offline is enabled, otherwise uses the real-time API as we normally do today. When this mode is enabled, we also switch to local storage over session storage to persist 'sessions' loaded from the user's homescreen.How does it work?
Previous efforts to add offline caching for our content have assumed we would be working against the full, real-time Firebase API (which, for HN means updates flowing in every few seconds). This was complex to manage and quite susceptible to slowness, IDB thrashing (even from a Web Worker) and didn't give us what we needed on mobile.
Instead, I've introduced
HNServiceRest. This special-cases Offline Mode so that it switches to using the Firebase REST API (which just gives you a bunch of.jsonURLs for content entries). This is trivial for us to now cache at runtime usingsw-toolbox(which we've already been including).What browsers has this been tested in?
Offline Mode has been tested as working in both Chrome, Opera and Firefox. In browsers without support for Service Worker (e.g Safari stable), it will still work the same way as it does today. I had considered hiding the
Offline Modeoption if SW wasn't supported, but might think about doing this as a follow-up.Does this negatively impact the default experience in any way?
As
Offline Modeneeds to be manually switched on, the default experience for all users will still be real-time data from the Firebase HN endpoint. As long as I've got the casing right, this feature should be considered an enhancement.cc @insin @NekR