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
Theinertia_response! macro takes a component name and props, and returns the appropriate response format:
InertiaProps Derive Macro
TheInertiaProps derive macro automatically implements Serialize for your props struct:
Compile-Time Component Validation
Theinertia_response! macro validates at compile time that your component exists:
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: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
Theinertia_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
| Feature | Description |
|---|---|
inertia_response! | Macro to return Inertia responses |
InertiaProps | Derive macro for props serialization |
| Compile-time validation | Checks component exists at build time |
| Automatic format | Returns HTML or JSON based on request |
| Nested props | Support for complex data structures |