The Reference Implementation
The official Laravel adapter is the reference implementation of the protocol, and every example in this documentation uses it. Where the protocol leaves a choice to the server, follow the Laravel adapter unless your framework has a strong reason to differ.
The adapter’s test suite covers edge cases that the protocol describes only briefly, so porting the relevant tests is a quick way to confirm that your adapter behaves the same way.
The Basics
A basic Inertia application needs a render function, a root template helper, and a middleware from your adapter. The render function accepts a component name and props, builds the page object, and returns either an HTML response or an Inertia response. A minimal version looks like this.<x-inertia::app> and <x-inertia::head> Blade components. These helpers also print the output of server-side rendering.
The middleware handles most cross-cutting protocol behavior. The Laravel adapter’s Middleware::handle() method does the following:
- Sets the
Vary: X-Inertiaheader on every response. - Returns a
409on an asset version mismatch for InertiaGETrequests. - Converts a
302redirect after aPUT,PATCH,DELETE, orQUERYrequest into a303. - Converts a redirect to a URL fragment into an
X-Inertia-Redirectresponse. - Redirects back when a controller returns an empty
200 OKresponse. - Reflashes the session’s flash data on redirects and version mismatches.
- Registers shared data, including the validation errors from the session.
Verifying Your Adapter
Unit tests confirm that your adapter emits the right headers and page objects, but not that the official client accepts them. Build a small application with one of the official client-side adapters and check the following:- The first page load renders the page component.
- Clicking a
<Link>swaps the component without a full page reload. - Submitting a form with invalid data redirects back and shows the validation errors.
- A partial reload returns only the requested props and the
errorsprop. - Changing the asset version makes the next visit reload the full page.
Additional Features
Once the basics work, you may add any of the following features.Best Practices
Follow Your Framework’s Conventions
Follow your framework’s naming, configuration style, and extension points, so a Rails adapter reads like Rails and a Django adapter reads like Django. Keep the concepts close to the Laravel adapter’s, such asrender, share, defer, and merge, so this documentation stays useful to your users.
Build Both Responses From One Code Path
Build the page object for the HTML document and for the JSON response in one code path. Any difference between the two causes bugs that appear only on the first visit or only after navigation.Compose Prop Types
A single prop may be both deferred and mergeable, or once and deferred. Modeling each behavior as an independent flag or trait, rather than as one class per combination, keeps these combinations simple.Encode Empty Errors as Objects
Encode theerrors prop and each error bag as JSON objects, even when they are empty. Languages that use one type for lists and maps, such as PHP, encode an empty one as [] by default, but the protocol expects {}.
Render Error Pages Through Inertia
Exceptions such as404 and 500 errors are often rendered outside your middleware, so they lose shared data. Provide a way to render them as Inertia responses with the middleware’s shared data, like the Laravel adapter’s Inertia::handleExceptionsUsing() method.
Run Post-Response Work for Client Redirects
A409 for an external redirect or a fragment redirect is a successful request, because the client completes the navigation. Some frameworks skip post-response work, such as after-response callbacks, for 4xx statuses, so make sure your adapter still runs it for these responses.