Server-Side Setup
The first step when installing Inertia is to configure your server-side framework. Inertia maintains official server-side adapters for Laravel (both PHP and Rust), and the community maintains adapters for Rails, Django, Phoenix, and many other frameworks.
Inertia is fine-tuned for Laravel, so the documentation examples on this website utilize Laravel PHP and Laravel Rust. For examples of using Inertia with other server-side frameworks, please refer to the framework-specific documentation maintained by the relevant adapter.
Installation
1. Install dependencies
Install the Inertia server-side adapter using the Composer package manager.
1composer require inertiajs/inertia-laravel1composer require inertiajs/inertia-laravel
Inertia support is built into the framework via the inertia Cargo feature, which is enabled by default. New Laravel Rust applications created with laravel new --rust come pre-configured with Inertia, React, TypeScript, Tailwind, Vite Plus, HandleInertiaRequests in src/http/middleware, and resources/views/app.html, so no separate installation is required.
2. Setup root template
Next, create your resources/views/app.blade.phpresources/views/app.html root template. This root template will be loaded on the first page visit to your application.
1<html>2 <head>3 <meta charset="utf-8" />4 <meta name="viewport" content="width=device-width, initial-scale=1">5 @vite('resources/js/app.js')6 <x-inertia::head />7 </head>8 <body>9 <x-inertia::app />10 </body>11</html>1<html>2 <head>3 <meta charset="utf-8" />4 <meta name="viewport" content="width=device-width, initial-scale=1">5 @vite('resources/js/app.js')6 <x-inertia::head />7 </head>8 <body>9 <x-inertia::app />10 </body>11</html>
1<!DOCTYPE html>2<html lang="en">3 <head>4 <meta charset="utf-8" />5 <meta name="viewport" content="width=device-width, initial-scale=1">6 {{ vite_react_refresh() }}7 {{ vite("resources/js/app.tsx", "resources/js/pages/" ~ page.component ~ ".tsx") }}8 {{ inertia_head }}9 </head>10 <body>11 {{ inertia }}12 </body>13</html>1<!DOCTYPE html>2<html lang="en">3 <head>4 <meta charset="utf-8" />5 <meta name="viewport" content="width=device-width, initial-scale=1">6 {{ vite_react_refresh() }}7 {{ vite("resources/js/app.tsx", "resources/js/pages/" ~ page.component ~ ".tsx") }}8 {{ inertia_head }}9 </head>10 <body>11 {{ inertia }}12 </body>13</html>
For React applications, it's recommended to include the @viteReactRefresh directive before the @vite directive to enable Fast Refresh in development, and you may pass the current page component to @vite to preload the initial page.
For React applications, {{ vite_react_refresh() }} enables Fast Refresh in development, and passing the current page component to {{ vite(...) }} preloads the initial page.
By default, Inertia renders a <div> element with an id of app as the mounting point for your JavaScript application. You may customize the id via the <x-inertia::app> componentHandleInertiaRequests::configure.
1<x-inertia::app id="custom-app-id" />1<x-inertia::app id="custom-app-id" />
1use laravel::inertia;23impl inertia::Middleware for HandleInertiaRequests {4 fn configure(&self, config: inertia::Config) -> inertia::Config {5 config.root_id("custom-app-id")6 }7}1use laravel::inertia;23impl inertia::Middleware for HandleInertiaRequests {4 fn configure(&self, config: inertia::Config) -> inertia::Config {5 config.root_id("custom-app-id")6 }7}
If you change the id of the root element, be sure to update it client-side as well.
The <x-inertia::head> component also accepts fallback content via its slot for managing head elements when SSR is not active.
By default, Inertia assumes your root template is named app (resources/views/app.blade.phpresources/views/app.html). You may change this using Inertia::setRootView()by overriding root_view on HandleInertiaRequests.
1Inertia::setRootView('layouts.inertia');1Inertia::setRootView('layouts.inertia');
1use laravel::inertia::{self, RootPage};2use laravel::view::Html;34impl inertia::Middleware for HandleInertiaRequests {5 fn root_view(&self, page: &RootPage<'_>) -> laravel::Result<Html> {6 page.view("layouts.inertia").render()7 }8}1use laravel::inertia::{self, RootPage};2use laravel::view::Html;34impl inertia::Middleware for HandleInertiaRequests {5 fn root_view(&self, page: &RootPage<'_>) -> laravel::Result<Html> {6 page.view("layouts.inertia").render()7 }8}
3. Register middleware
Next, publish the HandleInertiaRequests middleware to your application with Artisan:
1php artisan inertia:middleware1php artisan inertia:middleware
Next, set up the HandleInertiaRequests middleware. New applications already include HandleInertiaRequests in src/http/middleware implementing the inertia::Middleware trait:
1use laravel::inertia::{self, Props, props};2use laravel::prelude::*;34pub struct HandleInertiaRequests;56impl inertia::Middleware for HandleInertiaRequests {7 async fn share(&self, _request: &Request) -> laravel::Result<Props> {8 Ok(props! {9 name => laravel::config().app.name.clone(),10 })11 }1213 fn error_page(&self) -> Option<&str> {14 Some("Error")15 }16}1use laravel::inertia::{self, Props, props};2use laravel::prelude::*;34pub struct HandleInertiaRequests;56impl inertia::Middleware for HandleInertiaRequests {7 async fn share(&self, _request: &Request) -> laravel::Result<Props> {8 Ok(props! {9 name => laravel::config().app.name.clone(),10 })11 }1213 fn error_page(&self) -> Option<&str> {14 Some("Error")15 }16}
Once the middleware is in place, append HandleInertiaRequests to the web middleware group in bootstrap/app.phpsrc/app.rs.
1use App\Http\Middleware\HandleInertiaRequests;23->withMiddleware(function (Middleware $middleware) {4 $middleware->web(append: [5 HandleInertiaRequests::class,6 ]);7})1use App\Http\Middleware\HandleInertiaRequests;23->withMiddleware(function (Middleware $middleware) {4 $middleware->web(append: [5 HandleInertiaRequests::class,6 ]);7})
1use laravel::inertia;23use crate::http::middleware::HandleInertiaRequests;45.with_middleware(|middleware, _| {6 middleware.web(|web| {7 web.append(inertia::middleware(HandleInertiaRequests));8 });9})1use laravel::inertia;23use crate::http::middleware::HandleInertiaRequests;45.with_middleware(|middleware, _| {6 middleware.web(|web| {7 web.append(inertia::middleware(HandleInertiaRequests));8 });9})
This middleware provides a version() method for setting your asset version, as well as a share() method for defining shared data.
4. Create your first response
That's it, you're ready to start creating Inertia pages and rendering them via responses.
1use Inertia\Inertia;23class EventsController extends Controller4{5 public function show(Event $event)6 {7 return Inertia::render('Event/Show', [8 'event' => $event->only(9 'id',10 'title',11 'start_date',12 'description',13 ),14 ]);15 }16}1use Inertia\Inertia;23class EventsController extends Controller4{5 public function show(Event $event)6 {7 return Inertia::render('Event/Show', [8 'event' => $event->only(9 'id',10 'title',11 'start_date',12 'description',13 ),14 ]);15 }16}
1use laravel::inertia::{self, Page, props};23use crate::models::Event;45pub async fn show(event: Event) -> Page {6 inertia::render("Event/Show", props! { event })7}1use laravel::inertia::{self, Page, props};23use crate::models::Event;45pub async fn show(event: Event) -> Page {6 inertia::render("Event/Show", props! { event })7}
Blade Directives
In Laravel PHP, prior to v3, Inertia used @inertiaHead and @inertia Blade directives to render head and body content. These directives are still supported but do not offer SSR head fallback content. The Blade components above are recommended for new Laravel PHP applications.
1<html>2 <head>3 @vite('resources/js/app.js')4 @inertiaHead5 </head>6 <body>7 @inertia8 </body>9</html>1<html>2 <head>3 @vite('resources/js/app.js')4 @inertiaHead5 </head>6 <body>7 @inertia8 </body>9</html>
You may customize the root element id by passing a value to the directive: @inertia('custom-app-id').
Other Frameworks
Inertia works with any server-side framework that implements the Inertia protocol. You may use one of the community adapters or build your own.