Skip to main content
Kit provides the inertia_response! macro for returning Inertia responses from your controllers. This macro handles both initial page loads (HTML) and subsequent XHR requests (JSON).

The inertia_response! Macro

The inertia_response! macro takes a component name and props, and returns the appropriate response format:

InertiaProps Derive Macro

The InertiaProps derive macro automatically implements Serialize for your props struct:
This is equivalent to:

Compile-Time Component Validation

The inertia_response! macro validates at compile time that your component exists:
Components are resolved from frontend/src/pages/{component}.tsx.

Nested Components

For nested page components, use the full path:

JSON-Style Props

You can also use JSON-style syntax for simple cases:
However, typed props are recommended for type safety and TypeScript generation.

Complex Props

Props can contain nested structs, vectors, and optional values:

Fetching Data from Database

Combine Inertia responses with database queries:

How Response Format Works

The inertia_response! macro automatically detects whether to return HTML or JSON:

Initial Page Load

When a user navigates directly to a URL:

XHR Navigation

When Inertia makes an XHR request:

Configuration

Inertia behavior is configured via environment variables:

InertiaConfig

You can also configure programmatically:

Best Practices

Keep Props Flat When Possible

Use Option for Nullable Values

Avoid Sending Sensitive Data

Summary