Skip to content

Pages

WARNING You're browsing the documentation for an old version of Inertia. Consider upgrading your project to Inertia 3.x.

The Basics

Pages

When building applications using Inertia, each page in your application typically has its own controller / 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 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 export let user
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 export let user
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>
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 or route. In this example, let's assume this page is stored at resources/js/Pages/User/Show.vueresources/js/Pages/User/Show.jsxresources/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}

If you attempt to render a page that does not exist, the response will typically be a blank screen. To prevent this, you may set the inertia.ensure_pages_exist configuration option to true. The Laravel adapter will then throw an Inertia\ComponentNotFoundException when a page cannot be found.

Creating Layouts

While not required, for most projects it makes sense to create a layout component that all of your pages can use. You may have noticed in our page example above that we're wrapping the page content within a <Layout> component. Here's an example of such a component:

1<script setup>
2import { Link } from '@inertiajs/vue3'
3</script>
4 
5<template>
6 <main>
7 <header>
8 <Link href="/">Home</Link>
9 <Link href="/about">About</Link>
10 <Link href="/contact">Contact</Link>
11 </header>
12 <article>
13 <slot />
14 </article>
15 </main>
16</template>
1<script setup>
2import { Link } from '@inertiajs/vue3'
3</script>
4 
5<template>
6 <main>
7 <header>
8 <Link href="/">Home</Link>
9 <Link href="/about">About</Link>
10 <Link href="/contact">Contact</Link>
11 </header>
12 <article>
13 <slot />
14 </article>
15 </main>
16</template>
1import { Link } from '@inertiajs/react'
2 
3export default function Layout({ children }) {
4 return (
5 <main>
6 <header>
7 <Link href="/">Home</Link>
8 <Link href="/about">About</Link>
9 <Link href="/contact">Contact</Link>
10 </header>
11 <article>{children}</article>
12 </main>
13 )
14}
1import { Link } from '@inertiajs/react'
2 
3export default function Layout({ children }) {
4 return (
5 <main>
6 <header>
7 <Link href="/">Home</Link>
8 <Link href="/about">About</Link>
9 <Link href="/contact">Contact</Link>
10 </header>
11 <article>{children}</article>
12 </main>
13 )
14}
1<script>
2 import { inertia } from '@inertiajs/svelte'
3</script>
4 
5<main>
6 <header>
7 <a use:inertia href="/">Home</a>
8 <a use:inertia href="/about">About</a>
9 <a use:inertia href="/contact">Contact</a>
10 </header>
11 <article>
12 <slot />
13 </article>
14</main>
1<script>
2 import { inertia } from '@inertiajs/svelte'
3</script>
4 
5<main>
6 <header>
7 <a use:inertia href="/">Home</a>
8 <a use:inertia href="/about">About</a>
9 <a use:inertia href="/contact">Contact</a>
10 </header>
11 <article>
12 <slot />
13 </article>
14</main>
1<script>
2 import { inertia } from '@inertiajs/svelte'
3 
4 let { children } = $props()
5</script>
6 
7<main>
8 <header>
9 <a use:inertia href="/">Home</a>
10 <a use:inertia href="/about">About</a>
11 <a use:inertia href="/contact">Contact</a>
12 </header>
13 <article>
14 {@render children()}
15 </article>
16</main>
1<script>
2 import { inertia } from '@inertiajs/svelte'
3 
4 let { children } = $props()
5</script>
6 
7<main>
8 <header>
9 <a use:inertia href="/">Home</a>
10 <a use:inertia href="/about">About</a>
11 <a use:inertia href="/contact">Contact</a>
12 </header>
13 <article>
14 {@render children()}
15 </article>
16</main>

As you can see, there is nothing Inertia specific within this template. This is just a typical VueReactSvelte component.

Persistent Layouts

While it's simple to implement layouts as children of page components, it forces the layout instance to be destroyed and recreated between visits. This means you cannot have persistent layout state when navigating between pages.

For example, maybe you have an audio player on a podcast website that you want to continue playing as users navigate the site. Or, maybe you simply want to maintain the scroll position in your sidebar navigation between page visits. In these situations, the solution is to leverage Inertia's persistent layouts.

1<script>
2import Layout from './Layout'
3 
4export default {
5 // Using a render function...
6 layout: (h, page) => h(Layout, [page]),
7 
8 // Using shorthand syntax...
9 layout: Layout,
10}
11</script>
12 
13<script setup>
14defineProps({ user: Object })
15</script>
16 
17<template>
18 <h1>Welcome</h1>
19 <p>Hello {{ user.name }}, welcome to your first Inertia app!</p>
20</template>
1<script>
2import Layout from './Layout'
3 
4export default {
5 // Using a render function...
6 layout: (h, page) => h(Layout, [page]),
7 
8 // Using shorthand syntax...
9 layout: Layout,
10}
11</script>
12 
13<script setup>
14defineProps({ user: Object })
15</script>
16 
17<template>
18 <h1>Welcome</h1>
19 <p>Hello {{ user.name }}, welcome to your first Inertia app!</p>
20</template>
1import Layout from './Layout'
2 
3const Home = ({ user }) => {
4 return (
5 
6 <H1>Welcome</H1>
7 <p>Hello {user.name}, welcome to your first Inertia app!</p>
8 
9 )
10}
11 
12Home.layout = page => <Layout children={page} title="Welcome" />
13 
14export default Home
1import Layout from './Layout'
2 
3const Home = ({ user }) => {
4 return (
5 
6 <H1>Welcome</H1>
7 <p>Hello {user.name}, welcome to your first Inertia app!</p>
8 
9 )
10}
11 
12Home.layout = page => <Layout children={page} title="Welcome" />
13 
14export default Home
1<script context="module">
2 export { default as layout } from './Layout.svelte'
3</script>
4 
5<script>
6 export let user
7</script>
8 
9<h1>Welcome</h1>
10<p>Hello {user.name}, welcome to your first Inertia app!</p>
1<script context="module">
2 export { default as layout } from './Layout.svelte'
3</script>
4 
5<script>
6 export let user
7</script>
8 
9<h1>Welcome</h1>
10<p>Hello {user.name}, welcome to your first Inertia app!</p>
1<script module>
2 export { default as layout } from './Layout.svelte'
3</script>
4 
5<script>
6 let { user } = $props()
7</script>
8 
9<h1>Welcome</h1>
10<p>Hello {user.name}, welcome to your first Inertia app!</p>
1<script module>
2 export { default as layout } from './Layout.svelte'
3</script>
4 
5<script>
6 let { user } = $props()
7</script>
8 
9<h1>Welcome</h1>
10<p>Hello {user.name}, welcome to your first Inertia app!</p>

You can also create more complex layout arrangements using nested layouts.

1<script>
2import SiteLayout from './SiteLayout'
3import NestedLayout from './NestedLayout'
4 
5export default {
6 // Using a render function...
7 layout: (h, page) => {
8 return h(SiteLayout, () => h(NestedLayout, () => page))
9 },
10 
11 // Using the shorthand...
12 layout: [SiteLayout, NestedLayout],
13}
14</script>
15 
16<script setup>
17defineProps({ user: Object })
18</script>
19 
20<template>
21 <h1>Welcome</h1>
22 <p>Hello {{ user.name }}, welcome to your first Inertia app!</p>
23</template>
1<script>
2import SiteLayout from './SiteLayout'
3import NestedLayout from './NestedLayout'
4 
5export default {
6 // Using a render function...
7 layout: (h, page) => {
8 return h(SiteLayout, () => h(NestedLayout, () => page))
9 },
10 
11 // Using the shorthand...
12 layout: [SiteLayout, NestedLayout],
13}
14</script>
15 
16<script setup>
17defineProps({ user: Object })
18</script>
19 
20<template>
21 <h1>Welcome</h1>
22 <p>Hello {{ user.name }}, welcome to your first Inertia app!</p>
23</template>
1import SiteLayout from './SiteLayout'
2import NestedLayout from './NestedLayout'
3 
4const Home = ({ user }) => {
5 return (
6 
7 <H1>Welcome</H1>
8 <p>Hello {user.name}, welcome to your first Inertia app!</p>
9 
10 )
11}
12 
13Home.layout = page => (
14 <SiteLayout title="Welcome">
15 <NestedLayout children={page} />
16 </SiteLayout>
17)
18 
19export default Home
1import SiteLayout from './SiteLayout'
2import NestedLayout from './NestedLayout'
3 
4const Home = ({ user }) => {
5 return (
6 
7 <H1>Welcome</H1>
8 <p>Hello {user.name}, welcome to your first Inertia app!</p>
9 
10 )
11}
12 
13Home.layout = page => (
14 <SiteLayout title="Welcome">
15 <NestedLayout children={page} />
16 </SiteLayout>
17)
18 
19export default Home
1<script context="module">
2 import SiteLayout from './SiteLayout.svelte'
3 import NestedLayout from './NestedLayout.svelte'
4 
5 // Using a render function...
6 export const layout = (h, page) => {
7 return h(SiteLayout, [h(NestedLayout, [page])])
8 }
9 
10 // Using the shorthand...
11 export const layout = [SiteLayout, NestedLayout]
12</script>
13 
14<script>
15 export let user
16</script>
17 
18<h1>Welcome</h1>
19<p>Hello {user.name}, welcome to your first Inertia app!</p>
1<script context="module">
2 import SiteLayout from './SiteLayout.svelte'
3 import NestedLayout from './NestedLayout.svelte'
4 
5 // Using a render function...
6 export const layout = (h, page) => {
7 return h(SiteLayout, [h(NestedLayout, [page])])
8 }
9 
10 // Using the shorthand...
11 export const layout = [SiteLayout, NestedLayout]
12</script>
13 
14<script>
15 export let user
16</script>
17 
18<h1>Welcome</h1>
19<p>Hello {user.name}, welcome to your first Inertia app!</p>
1<script module>
2 import SiteLayout from './SiteLayout.svelte'
3 import NestedLayout from './NestedLayout.svelte'
4 
5 // Using a render function...
6 export const layout = (h, page) => {
7 return h(SiteLayout, [h(NestedLayout, [page])])
8 }
9 
10 // Using the shorthand...
11 export const layout = [SiteLayout, NestedLayout]
12</script>
13 
14<script>
15 let { user } = $props()
16</script>
17 
18<h1>Welcome</h1>
19<p>Hello {user.name}, welcome to your first Inertia app!</p>
1<script module>
2 import SiteLayout from './SiteLayout.svelte'
3 import NestedLayout from './NestedLayout.svelte'
4 
5 // Using a render function...
6 export const layout = (h, page) => {
7 return h(SiteLayout, [h(NestedLayout, [page])])
8 }
9 
10 // Using the shorthand...
11 export const layout = [SiteLayout, NestedLayout]
12</script>
13 
14<script>
15 let { user } = $props()
16</script>
17 
18<h1>Welcome</h1>
19<p>Hello {user.name}, welcome to your first Inertia app!</p>

If you're using Vue 3.3+, you can alternatively use defineOptions to define a layout within <script setup>. Older versions of Vue can use the defineOptions plugin:

1<script setup>
2import Layout from './Layout'
3defineOptions({ layout: Layout })
4</script>
1<script setup>
2import Layout from './Layout'
3defineOptions({ layout: Layout })
4</script>

Default Layouts

If you're using persistent layouts, you may find it convenient to define the default page layout in the resolve() callback of your application's main JavaScript file.

1import Layout from './Layout'
2 
3createInertiaApp({
4 resolve: name => {
5 const pages = import.meta.glob('./Pages/**/*.vue', { eager: true })
6 let page = pages[`./Pages/${name}.vue`]
7 page.default.layout = page.default.layout || Layout
8 return page
9 },
10 // ...
11})
1import Layout from './Layout'
2 
3createInertiaApp({
4 resolve: name => {
5 const pages = import.meta.glob('./Pages/**/*.vue', { eager: true })
6 let page = pages[`./Pages/${name}.vue`]
7 page.default.layout = page.default.layout || Layout
8 return page
9 },
10 // ...
11})
1import Layout from './Layout'
2 
3createInertiaApp({
4 resolve: name => {
5 const pages = import.meta.glob('./Pages/**/*.jsx', { eager: true })
6 let page = pages[`./Pages/${name}.jsx`]
7 page.default.layout = page.default.layout || (page => <Layout children={page} />)
8 return page
9 },
10 // ...
11})
1import Layout from './Layout'
2 
3createInertiaApp({
4 resolve: name => {
5 const pages = import.meta.glob('./Pages/**/*.jsx', { eager: true })
6 let page = pages[`./Pages/${name}.jsx`]
7 page.default.layout = page.default.layout || (page => <Layout children={page} />)
8 return page
9 },
10 // ...
11})
1import Layout from './Layout'
2 
3createInertiaApp({
4 resolve: name => {
5 const pages = import.meta.glob('./Pages/**/*.svelte', { eager: true })
6 let page = pages[`./Pages/${name}.svelte`]
7 return { default: page.default, layout: page.layout || Layout }
8 },
9 // ...
10})
1import Layout from './Layout'
2 
3createInertiaApp({
4 resolve: name => {
5 const pages = import.meta.glob('./Pages/**/*.svelte', { eager: true })
6 let page = pages[`./Pages/${name}.svelte`]
7 return { default: page.default, layout: page.layout || Layout }
8 },
9 // ...
10})

This will automatically set the page layout to Layout if a layout has not already been set for that page.

You can even go a step further and conditionally set the default page layout based on the page name, which is available to the resolve() callback. For example, maybe you don't want the default layout to be applied to your public pages.

1import Layout from './Layout'
2 
3createInertiaApp({
4 resolve: name => {
5 const pages = import.meta.glob('./Pages/**/*.vue', { eager: true })
6 let page = pages[`./Pages/${name}.vue`]
7 page.default.layout = name.startsWith('Public/') ? undefined : Layout
8 return page
9 },
10 // ...
11})
1import Layout from './Layout'
2 
3createInertiaApp({
4 resolve: name => {
5 const pages = import.meta.glob('./Pages/**/*.vue', { eager: true })
6 let page = pages[`./Pages/${name}.vue`]
7 page.default.layout = name.startsWith('Public/') ? undefined : Layout
8 return page
9 },
10 // ...
11})
1import Layout from './Layout'
2 
3createInertiaApp({
4 resolve: name => {
5 const pages = import.meta.glob('./Pages/**/*.jsx', { eager: true })
6 let page = pages[`./Pages/${name}.jsx`]
7 page.default.layout = name.startsWith('Public/') ? undefined : page => <Layout children={page} />
8 return page
9 },
10 // ...
11})
1import Layout from './Layout'
2 
3createInertiaApp({
4 resolve: name => {
5 const pages = import.meta.glob('./Pages/**/*.jsx', { eager: true })
6 let page = pages[`./Pages/${name}.jsx`]
7 page.default.layout = name.startsWith('Public/') ? undefined : page => <Layout children={page} />
8 return page
9 },
10 // ...
11})
1import Layout from './Layout'
2 
3createInertiaApp({
4 resolve: name => {
5 const pages = import.meta.glob('./Pages/**/*.svelte', { eager: true })
6 let page = pages[`./Pages/${name}.svelte`]
7 return { default: page.default, layout: name.startsWith('Public/') ? undefined : Layout }
8 },
9 // ...
10})
1import Layout from './Layout'
2 
3createInertiaApp({
4 resolve: name => {
5 const pages = import.meta.glob('./Pages/**/*.svelte', { eager: true })
6 let page = pages[`./Pages/${name}.svelte`]
7 return { default: page.default, layout: name.startsWith('Public/') ? undefined : Layout }
8 },
9 // ...
10})