Skip to content

Pages

The Basics

Pages

When building applications using Inertia, each page in your application typically has its own controller, handler, or route and a corresponding JavaScript component. This allows you to retrieve just the data necessary for that page - no API required.

In addition, all of the data needed for the page can be retrieved before the page is ever rendered by the browser, eliminating the need for displaying "loading" states when users visit your application.

Creating Pages

Inertia pages are simply JavaScript components. If you have ever written a Vue, React, or Svelte component, you will feel right at home. As you can see in the example below, pages receive data from your application's controllers or handlers as props.

1<script setup>
2import Layout from "./Layout";
3import { Head } from "@inertiajs/vue3";
4 
5defineProps({ user: Object });
6</script>
7 
8<template>
9 <Layout>
10 <Head title="Welcome" />
11 <h1>Welcome</h1>
12 <p>Hello {{ user.name }}, welcome to your first Inertia app!</p>
13 </Layout>
14</template>
1<script setup>
2import Layout from "./Layout";
3import { Head } from "@inertiajs/vue3";
4 
5defineProps({ user: Object });
6</script>
7 
8<template>
9 <Layout>
10 <Head title="Welcome" />
11 <h1>Welcome</h1>
12 <p>Hello {{ user.name }}, welcome to your first Inertia app!</p>
13 </Layout>
14</template>
1import Layout from "./Layout";
2import { Head } from "@inertiajs/react";
3 
4export default function Welcome({ user }) {
5 return (
6 <Layout>
7 <Head title="Welcome" />
8 <h1>Welcome</h1>
9 <p>Hello {user.name}, welcome to your first Inertia app!</p>
10 </Layout>
11 );
12}
1import Layout from "./Layout";
2import { Head } from "@inertiajs/react";
3 
4export default function Welcome({ user }) {
5 return (
6 <Layout>
7 <Head title="Welcome" />
8 <h1>Welcome</h1>
9 <p>Hello {user.name}, welcome to your first Inertia app!</p>
10 </Layout>
11 );
12}
1<script>
2 import Layout from './Layout.svelte'
3 
4 let { user } = $props()
5</script>
6 
7<svelte:head>
8 <title>Welcome</title>
9</svelte:head>
10 
11<Layout>
12 <h1>Welcome</h1>
13 <p>Hello {user.name}, welcome to your first Inertia app!</p>
14</Layout>
1<script>
2 import Layout from './Layout.svelte'
3 
4 let { user } = $props()
5</script>
6 
7<svelte:head>
8 <title>Welcome</title>
9</svelte:head>
10 
11<Layout>
12 <h1>Welcome</h1>
13 <p>Hello {user.name}, welcome to your first Inertia app!</p>
14</Layout>

Given the page above, you can render the page by returning an Inertia response from a controller, handler, or route. In this example, let's assume this page is stored at resources/js/Pages/User/Show.vue resources/js/Pages/User/Show.jsx resources/js/Pages/User/Show.sveltewithin a Laravel application.

1use Inertia\Inertia;
2 
3class UserController extends Controller
4{
5 public function show(User $user)
6 {
7 return Inertia::render('User/Show', [
8 'user' => $user
9 ]);
10 }
11}
1use Inertia\Inertia;
2 
3class UserController extends Controller
4{
5 public function show(User $user)
6 {
7 return Inertia::render('User/Show', [
8 'user' => $user
9 ]);
10 }
11}
1use laravel::inertia::{self, Page, props};
2 
3use crate::models::User;
4 
5pub async fn show(user: User) -> Page {
6 inertia::render("User/Show", props! { user })
7}
1use laravel::inertia::{self, Page, props};
2 
3use crate::models::User;
4 
5pub async fn show(user: User) -> Page {
6 inertia::render("User/Show", props! { user })
7}

If you attempt to render a page that does not exist, the response will typically be a blank screen.

You may set the inertia.ensure_pages_exist configuration option to true, and the adapter will throw an Inertia\ComponentNotFoundException when a page cannot be found.

Transforming Component Names

Sometimes you may wish to transform component names. This may be done on the client side using the pages.transform option, or on the server using the Inertia::transformComponentUsing() method.

The transformed name is sent to the browser and used for ensure_pages_exist checks.

1use Inertia\Inertia;
2 
3Inertia::transformComponentUsing(function (string $name): string {
4 return "{$name}/Page";
5});
1use Inertia\Inertia;
2 
3Inertia::transformComponentUsing(function (string $name): string {
4 return "{$name}/Page";
5});

Layouts

Most applications share common UI elements across pages. Inertia provides persistent layouts that survive page navigations, along with layout props for passing dynamic data between pages and their layouts. Visit the layouts documentation to learn more.