Instawork / hyperview

Server-driven mobile apps with React Native
https://hyperview.org
MIT License
1.28k stars 66 forks source link

Offline data #445

Open ivangretsky opened 2 years ago

ivangretsky commented 2 years ago

Good day!

I am investigating hyperview and really like the concept (being a web dev)) I am eager to bring this tech to our project, as I the our current team can handle the content generation for the app the way we are used to work. But we have a requirement that the app users should have the ability to browse pages they already visited when the are offline. Probably with a notification that they are seeing an outdated previously loaded data.

I've read on the site that local/offline data is not supported in hyperview. Is this about the case I've described? Is it right that there is no way to cache loaded pages and show them from local storage when offline? Or is it about some other more complicated stuff?

If it is about my case, why is such caching not available? Is it just not implemented, or there is something preventing that to be done?

Thank you!

adamstep commented 2 years ago

Hi @ivangretsky . You are right, there's no theoretical reason why Hyperview could not cache requests and serve them when the client is offline. It just hasn't been implemented yet. I even explored some ways to cache screens using HTTP semantics, you can see the work here:

https://github.com/Instawork/hyperview/compare/adam/cache2

In that branch, I was trying to also implement stale-while-revalidating and stale-if-error directives in the Cache-Control header. This would allow Hyperview to show cached content while still fetching a fresh response from the server.

That PR may be overkill for you. The good news is that you can add your own implementation of offline caching without a change to the core library. As you can see in our demo app, the Hyperview React Native component takes an implementation of fetch as a prop. This function is used to make all HTTP requests to load screens and fragments. In the demo, we wrap the default fetch() functionality to add some request headers. However, you could implement a much more sophisticated version of fetch() that includes reading and writing responses to a cache you maintain yourself. In fact, in my proof-of-concept, I did something similar.

I hope that helps, let me know if you have any questions.

ivangretsky commented 2 years ago

Thanks for a detailed answer. It is a petty I am not in the know to use any of your suggestions, as I am not skilled enough for that. But just maybe we could hire someone in the know to handle this.

You are right, there's no theoretical reason why Hyperview could not cache requests and serve them when the client is offline.

Is there any practical reason that didn't happen yet? ) It seems like you already know how to handle this and even have some code. Is this on the roadmap? If so, when is it likely to be implemented? Is there anything someone from the outside can do to encourage you to do it?)

adamstep commented 2 years ago

Is there any practical reason that didn't happen yet? ) It seems like you already know how to handle this and even have some code. Is this on the roadmap? If so, when is it likely to be implemented? Is there anything someone from the outside can do to encourage you to do it?)

The code I wrote is exploratory, some parts of it worked but it wasn't a full solution. I would probably need to spend a few more weeks to turn it into a robust solution. My contributions to Hyperview tend to be driven by the feature priorities at the company where I work, Instawork. Our app does not have a requirement for offline capabilities, so it hasn't been high priority on the roadmap. The next big feature we want to add is client-side form validation, as documented here: https://github.com/Instawork/hyperview/discussions/332

ivangretsky commented 2 years ago

Thanks for the explanation) As far as I can tell Instawork is the main driving force for hyperview, so I hope it needs this one soon))))