hotwired / turbo-android

Android framework for making Turbo native apps
MIT License
425 stars 51 forks source link

On cold boots, don't call `webView.reload()` if the webView has no current URL #161

Closed ghiculescu closed 3 years ago

ghiculescu commented 3 years ago

The functionality added in https://github.com/hotwired/turbo-android/pull/138 (as well as my workaround in https://github.com/hotwired/turbo-android/issues/137) had a bug: if the webView has no URL loaded, calling refresh will result in a spinner appearing forever, and no requests being made to the server. The fix is to not call webView.reload() if webView.url is null. Instead just load the URL as normal.

To replicate:

Our real life example was that we had a call to refresh() in onResume that ran if the app had not been open for a while, to ensure fresh content. In some cases this would result in the app never launching, just showing a spinner forever. My hypothesis is this happens if the OS kills the app in the background. I have included a simplified example of our code in the demo, but don't mind if it gets removed before merging.

(Is there a better approach to this? How does Basecamp/Hey ensure fresh content on app load - either because the app was in memory in the background for a while or because Turbo/the Webview has cached the last screen?)

jayohms commented 3 years ago

@ghiculescu Thanks for outlining the issue you ran into. I've fixed this bug in a different way in #179. Calls to refresh() will now be ignored if the WebView is not initialized, since it leads to unintended behavior.

Is there a better approach to this? How does Basecamp/Hey ensure fresh content on app load - either because the app was in memory in the background for a while or because Turbo/the Webview has cached the last screen

We typically don't always ensure fresh content for the current screen when resuming the app. However, on screens where the content changes frequently, such as comment threads or Campfire chats, the WebView page uses a websocket connection through Hotwire/ActionCable to load the latest changes.

Since the heuristics for ensuring fresh content are going to vary so widely between apps, I doubt we'll add specific behavior directly in the turbo-android library.