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 (Rust, Go, and PHP), 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 Rust, Laravel Go, and Laravel PHP. 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

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.

Inertia support is built into the framework in the go.laravel.com/inertia package. New Laravel Go applications created with laravel new --go come pre-configured with Inertia, React, TypeScript, Tailwind, Vite Plus, inertia.Middleware in bootstrap/app.go, and resources/views/app.html, so no separate installation is required.

Install the Inertia server-side adapter using the Composer package manager.

1composer require inertiajs/inertia-laravel
1composer require inertiajs/inertia-laravel

2. Setup root template

Next, create your resources/views/app.htmlresources/views/app.htmlresources/views/app.blade.php root template. This root template will be loaded on the first page visit to your application.

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>
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 {{viteReactRefresh}}
7 {{vite "resources/js/app.tsx" (print "resources/js/pages/" .Component ".tsx")}}
8 {{.Head}}
9 </head>
10 <body>
11 {{.Body}}
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 {{viteReactRefresh}}
7 {{vite "resources/js/app.tsx" (print "resources/js/pages/" .Component ".tsx")}}
8 {{.Head}}
9 </head>
10 <body>
11 {{.Body}}
12 </body>
13</html>
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>

For React applications, {{ vite_react_refresh() }} enables Fast Refresh in development, and passing the current page component to {{ vite(...) }} preloads the initial page.

For React applications, {{viteReactRefresh}} enables Fast Refresh in development, and passing the current page component to {{vite ...}} preloads the initial page.

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.

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 HandleInertiaRequests::configurethe RootID field of inertia.Middlewarethe <x-inertia::app> component.

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}
1m.Web.Append(inertia.Middleware{
2 RootID: "custom-app-id",
3})
1m.Web.Append(inertia.Middleware{
2 RootID: "custom-app-id",
3})
1<x-inertia::app id="custom-app-id" />
1<x-inertia::app id="custom-app-id" />

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.htmlresources/views/app.htmlresources/views/app.blade.php). You may change this by overriding root_view on HandleInertiaRequestsvia the RootView field of inertia.Middlewareusing Inertia::setRootView().

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}
1m.Web.Append(inertia.Middleware{
2 RootView: "layouts.inertia",
3})
1m.Web.Append(inertia.Middleware{
2 RootView: "layouts.inertia",
3})
1Inertia::setRootView('layouts.inertia');
1Inertia::setRootView('layouts.inertia');

3. Register 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}

In Laravel Go, you configure Inertia by appending inertia.Middleware directly to the web middleware group in bootstrap/app.go (after authentication middleware). New applications already include this middleware with a share function and an ErrorPage component configured.

Next, publish the HandleInertiaRequests middleware to your application with Artisan:

1php artisan inertia:middleware
1php artisan inertia:middleware

Once the middleware is in place, append HandleInertiaRequests to the web middleware group in src/app.rs.Append inertia.Middleware to the web middleware group in bootstrap/app.go.Once the middleware is in place, append HandleInertiaRequests to the web middleware group in bootstrap/app.php.

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})
bootstrap/app.go
1import (
2 laravel "go.laravel.com"
3 "go.laravel.com/config"
4 "go.laravel.com/inertia"
5 "go.laravel.com/web"
6)
7 
8Middleware: func(m *laravel.Middleware, cfg *config.Config) {
9 m.Web.Append(inertia.Middleware{
10 Share: func(r *web.Request) (inertia.Props, error) {
11 return inertia.Props{
12 "name": config.From(r).App.Name,
13 }, nil
14 },
15 ErrorPage: "Error",
16 })
17},
1import (
2 laravel "go.laravel.com"
3 "go.laravel.com/config"
4 "go.laravel.com/inertia"
5 "go.laravel.com/web"
6)
7 
8Middleware: func(m *laravel.Middleware, cfg *config.Config) {
9 m.Web.Append(inertia.Middleware{
10 Share: func(r *web.Request) (inertia.Props, error) {
11 return inertia.Props{
12 "name": config.From(r).App.Name,
13 }, nil
14 },
15 ErrorPage: "Error",
16 })
17},
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})

This middleware provides a version() method for setting your asset version, as well as a share() method for defining shared data.a Version field for setting your asset version, as well as a Share field for defining shared data.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 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}
1package events
2 
3import (
4 "go.laravel.com/inertia"
5 "go.laravel.com/model"
6 "go.laravel.com/web"
7 
8 "app/models"
9)
10 
11func Show(r *web.Request) *web.Response {
12 event, err := model.FromRoute[models.Event](r, "event")
13 if err != nil {
14 return web.Fail(err)
15 }
16 
17 return inertia.Render("Event/Show", inertia.Props{"event": event})
18}
1package events
2 
3import (
4 "go.laravel.com/inertia"
5 "go.laravel.com/model"
6 "go.laravel.com/web"
7 
8 "app/models"
9)
10 
11func Show(r *web.Request) *web.Response {
12 event, err := model.FromRoute[models.Event](r, "event")
13 if err != nil {
14 return web.Fail(err)
15 }
16 
17 return inertia.Render("Event/Show", inertia.Props{"event": event})
18}
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}

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.