3 Useful Tricks When Working With Turbo Android

Thanks for following along with my Turbo Android series. I’m going to finish up with some general tips.

1. Debugging the WebView

Sometimes you will want to debug via the webview. To do this, do the following:

Open Chrome and type in chrome://inspect in the URL bar

This URL will display every Chrome process allowing you to use the Chrome Development tools on your Turbo Native webview.

2. Keep 99% on the Server

Delegate to your server-side app as much as possible. It might be tempting to build many native screens, but only go native if necessary. By serving HTML over the wire, you can keep your app simply because you only have to make changes in one place.

You also benefit from making changes over the wire when you need without having to get your app approved by Apple.

Scenarios, where native performance is superior include maps and offline functionality. For example, Smart Strength’s workout builder is native because I want users to use swipe to delete/copy, SwiftUI Context Menu and more.

3. Variants

If you have a legacy app, you can use variants to render different pages for the same request.

In Rails, you can set the requested variant with the following code:

# application_controller.rb
before_action do
  request.variant = :native if turbo_native_app?
end

If you look at the turbo_native_app method in the source code, it just checks to see if there is a user agent string in called Turbo Native.

When you do that, you can create the same view but append html+native.erb

When you request, Rails will evaluate the agent string to decide what variant to display.

Let’s say you visit /posts

For normal web browsing, Rails will render index.html.erb, but if you are navigating from a Turbo Native app, it will render index.html+native.erb. If there is no variant, it falls back to the standard view. This means you can avoid lots of if turbo_native_app? statements in your views.

Bonus Trick: 4. Use Jumpstart Pro

If you can afford it, I would buy a Jumpstart Pro Android and iOS licence. This will save you a lot of development time, plus it comes with documentation and access to the GoRails Discord, where you ask questions. You will also learn a lot from the codebase.

Downsides

Every choice has trade-offs, and Turbo Native is no different.

You need to get comfortable with Turbo.js. It has garnered some fans and praise, but some don’t like it. Turbo Native hinges on the fact that you are going to use Turbo. If you plan to include lots of JS libraries in your project, write a small Stimulus/JQuery wrapper for each one and get used to listening for the custom Turbo events. However, the trade-off has proved worthwhile, especially compared to the previous approach.

Learn to Build Android, iOS and Rails Apps using Hotwire

© 2024 William Kennedy, Inc. All rights reserved.