Essentials / Hotwire Native

Docs Essentials

Hotwire Native

Hotwire also has a mobile side and Turbo Laravel provides some helpers to make integrating with it easier.

Turbo visits made by a Hotwire Native client send a custom User-Agent header, which contains Hotwire Native (or Turbo Native, for older apps). Using that header, we can detect in the backend that a request is coming from a Hotwire Native client instead of a regular web browser.

This is useful if you want to customize the behavior slightly based on that information. For instance, you may want to include some elements only for mobile users, like a mobile-only CSS stylesheet. To do so, use the is_native() template function:

jinja
{% if is_native() %}
    <link rel="stylesheet" href="mobile.css">
{% endif %}

Or include some elements only when the client is NOT a Hotwire Native client:

jinja
{% if not is_native() %}
    <h1>Hello, Non-Hotwire Native Users!</h1>
{% endif %}

You may also check it in a handler, through the RequestExt trait:

rust
use laravel::http::Request;
use turbo::RequestExt;

if request.is_native() {
    // ...
}

Interacting With Hotwire Native Navigation

Hotwire Native hooks into Turbo's visits and displays them by stacking screens the way mobile apps do, instead of just replacing elements on the same screen. This helps our hybrid app feel native.

However, sometimes we may need to customize the behavior of form handlers to avoid a weird screen-jumping effect on the mobile client. Instead of regular redirects, we can send some signals by redirecting to specific routes that the Hotwire Native client detects.

For instance, if a form submission came from a Hotwire Native client, the form was probably rendered on a native modal, which is not part of the screen stack, so we can tell Turbo to refresh the current screen on the stack instead. There are 3 signals we can send to the Hotwire Native client:

Signal Route Description
recede /recede_historical_location Go back to the previous screen
resume /resume_historical_location Stay on the current screen as is
refresh /refresh_historical_location Stay on the current screen but refresh

Take a Native argument in your handler, and pass its methods the redirect browsers should get:

rust
use laravel::model::create;
use laravel::prelude::*;
use turbo::Native;

pub async fn store(native: Native, Valid(form): Valid<StoreTray>) -> laravel::Result<Redirect> {
    let tray = create!(Tray { name: form.name }).await?;

    Ok(native.recede_or(redirect!(trays::show, tray.id)).with("status", "Tray created."))
}

When the request comes from a Hotwire Native client, this redirects to /recede_historical_location. Otherwise, it redirects to the tray's page.

Method Native apps go to Browsers go to
native.recede_or(redirect) /recede_historical_location redirect
native.resume_or(redirect) /resume_historical_location redirect
native.refresh_or(redirect) /refresh_historical_location redirect
native.is_native() true false

The native redirect keeps the given redirect's query string, so a redirect to /trays/1?status=created becomes /recede_historical_location?status=created. In the Hotwire Native client, you may intercept these redirects, read the query string and show a native toast. Messages flashed with with go to the session either way, for the next page the app or the browser renders.

The provider registers the three routes. If you don't want them, set TURBO_NATIVE_ROUTES=false in your .env.