Skip to content

Server-Side Setup

Installation

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-laravel
1composer 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.

resources/views/app.blade.php
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>
resources/views/app.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;
2 
3impl 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;
2 
3impl 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;
3 
4impl 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;
3 
4impl 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:middleware
1php artisan inertia:middleware

Next, set up the HandleInertiaRequests middleware. New applications already include HandleInertiaRequests in src/http/middleware implementing the inertia::Middleware trait:

src/http/middleware/handle_inertia_requests.rs
1use laravel::inertia::{self, Props, props};
2use laravel::prelude::*;
3 
4pub struct HandleInertiaRequests;
5 
6impl 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 }
12 
13 fn error_page(&self) -> Option<&str> {
14 Some("Error")
15 }
16}
1use laravel::inertia::{self, Props, props};
2use laravel::prelude::*;
3 
4pub struct HandleInertiaRequests;
5 
6impl 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 }
12 
13 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.

bootstrap/app.php
1use App\Http\Middleware\HandleInertiaRequests;
2 
3->withMiddleware(function (Middleware $middleware) {
4 $middleware->web(append: [
5 HandleInertiaRequests::class,
6 ]);
7})
1use App\Http\Middleware\HandleInertiaRequests;
2 
3->withMiddleware(function (Middleware $middleware) {
4 $middleware->web(append: [
5 HandleInertiaRequests::class,
6 ]);
7})
src/app.rs
1use laravel::inertia;
2 
3use crate::http::middleware::HandleInertiaRequests;
4 
5.with_middleware(|middleware, _| {
6 middleware.web(|web| {
7 web.append(inertia::middleware(HandleInertiaRequests));
8 });
9})
1use laravel::inertia;
2 
3use crate::http::middleware::HandleInertiaRequests;
4 
5.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;
2 
3class EventsController extends Controller
4{
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;
2 
3class EventsController extends Controller
4{
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};
2 
3use crate::models::Event;
4 
5pub async fn show(event: Event) -> Page {
6 inertia::render("Event/Show", props! { event })
7}
1use laravel::inertia::{self, Page, props};
2 
3use crate::models::Event;
4 
5pub 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.

resources/views/app.blade.php
1<html>
2 <head>
3 @vite('resources/js/app.js')
4 @inertiaHead
5 </head>
6 <body>
7 @inertia
8 </body>
9</html>
1<html>
2 <head>
3 @vite('resources/js/app.js')
4 @inertiaHead
5 </head>
6 <body>
7 @inertia
8 </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.