How It Works¶
Understand the Inertia.js protocol and how it integrates with Litestar.
See also
Official Inertia.js docs: How It Works
The Inertia Protocol¶
Inertia.js is a protocol for building SPAs with server-side routing. Instead of returning JSON from an API, your routes return page components:
Initial page load: Server renders full HTML with embedded page data
Subsequent navigation: Client makes XHR request, server returns JSON
Client updates: Inertia swaps page component without full reload
sequenceDiagram
participant Browser
participant Server
Note over Browser,Server: Initial Page Load
Browser->>Server: GET /dashboard
Server->>Browser: HTML with data-page JSON
Note over Browser,Server: Subsequent Navigation
Browser->>Server: XHR GET /users<br>X-Inertia: true
Server->>Browser: JSON {component, props, url}
Note over Browser: Swap component, update URL
Request Flow¶
Initial Load (Full HTML):
<!-- Server returns full HTML -->
<div id="app" data-page='{"component":"Dashboard","props":{"user":"Alice"},"url":"/dashboard"}'></div>
XHR Request (JSON):
GET /users HTTP/1.1
X-Inertia: true
X-Inertia-Version: abc123
{
"component": "Users",
"props": {"users": ["Alice", "Bob"]},
"url": "/users",
"version": "abc123"
}
Inertia Headers¶
The protocol uses these HTTP headers:
Header |
Description |
|---|---|
|
Request: Client indicates Inertia request |
|
Request: Asset version for cache invalidation |
|
Request: Requested partial reload keys |
|
Request: Keys to exclude (v2) |
|
Request: Props to reset (v2) |
|
Response: Server confirms Inertia response |
|
Response: External redirect URL |
Version Checking¶
Inertia uses asset versioning to detect outdated clients:
Client sends
X-Inertia-VersionheaderServer compares with current manifest hash
On mismatch, server returns
409 ConflictClient performs full page reload
Litestar-Vite automatically handles versioning using the Vite manifest.
Litestar Integration¶
Litestar-Vite implements the protocol through:
InertiaPlugin: Middleware for request/response handling
InertiaResponse: Response class for page data
InertiaMiddleware: Header parsing and validation
ViteAssetLoader: Asset versioning via manifest hash
# Simple Inertia route
@get("/dashboard", component="Dashboard")
async def dashboard() -> dict:
return {"user": "Alice", "stats": {"views": 100}}
# The plugin handles:
# 1. Detecting X-Inertia header
# 2. Building PageProps object
# 3. Returning JSON or HTML based on request type
# 4. Version checking
See Also¶
The Inertia Protocol - Official protocol docs
Asset Versioning - Version checking details
Responses - Response classes