Skip to content

Shared Data

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

Data & Props

Shared Data

Sometimes you need to access specific pieces of data on numerous pages within your application. For example, you may need to display the current user in the site header. Passing this data manually in each response across your entire application is cumbersome. Thankfully, there is a better option: shared data.

Sharing Data

Inertia's server-side adapters all provide a method of making shared data available for every request. This is typically done outside of your controllers. Shared data will be automatically merged with the page props provided in your controller.

In Laravel applications, this is typically handled by the HandleInertiaRequests middleware that is automatically installed when installing the server-side adapter.

1class HandleInertiaRequests extends Middleware
2{
3 public function share(Request $request)
4 {
5 return array_merge(parent::share($request), [
6 // Synchronously...
7 'appName' => config('app.name'),
8 
9 // Lazily...
10 'auth.user' => fn () => $request->user()
11 ? $request->user()->only('id', 'name', 'email')
12 : null,
13 ]);
14 }
15}
1class HandleInertiaRequests extends Middleware
2{
3 public function share(Request $request)
4 {
5 return array_merge(parent::share($request), [
6 // Synchronously...
7 'appName' => config('app.name'),
8 
9 // Lazily...
10 'auth.user' => fn () => $request->user()
11 ? $request->user()->only('id', 'name', 'email')
12 : null,
13 ]);
14 }
15}

Alternatively, you can manually share data using the Inertia::share method.

1use Inertia\Inertia;
2 
3// Synchronously...
4Inertia::share('appName', config('app.name'));
5 
6// Lazily...
7Inertia::share('user', fn (Request $request) => $request->user()
8 ? $request->user()->only('id', 'name', 'email')
9 : null
10);
1use Inertia\Inertia;
2 
3// Synchronously...
4Inertia::share('appName', config('app.name'));
5 
6// Lazily...
7Inertia::share('user', fn (Request $request) => $request->user()
8 ? $request->user()->only('id', 'name', 'email')
9 : null
10);

Shared data should be used sparingly as all shared data is included with every response.

Page props and shared data are merged together, so be sure to namespace your shared data appropriately to avoid collisions.

Sharing Once Props

You may share data that is resolved only once and remembered by the client across subsequent navigations using once props.

1class HandleInertiaRequests extends Middleware
2{
3 public function share(Request $request)
4 {
5 return array_merge(parent::share($request), [
6 'countries' => Inertia::once(fn () => Country::all()),
7 ]);
8 }
9}
1class HandleInertiaRequests extends Middleware
2{
3 public function share(Request $request)
4 {
5 return array_merge(parent::share($request), [
6 'countries' => Inertia::once(fn () => Country::all()),
7 ]);
8 }
9}

Alternatively, you may define a dedicated shareOnce() method in the middleware. The middleware will evaluate both share() and shareOnce(), merging the results.

1class HandleInertiaRequests extends Middleware
2{
3 public function shareOnce(Request $request): array
4 {
5 return array_merge(parent::shareOnce($request), [
6 'countries' => fn () => Country::all(),
7 ]);
8 }
9}
1class HandleInertiaRequests extends Middleware
2{
3 public function shareOnce(Request $request): array
4 {
5 return array_merge(parent::shareOnce($request), [
6 'countries' => fn () => Country::all(),
7 ]);
8 }
9}

You may also share once props manually using the Inertia::shareOnce() method.

1Inertia::shareOnce('countries', fn () => Country::all());
1Inertia::shareOnce('countries', fn () => Country::all());

Accessing Shared Data

Once you have shared the data server-side, you will be able to access it within any of your pages or components. Here's an example of how to access shared data in a layout component.

1<script setup>
2import { computed } from "vue";
3import { usePage } from "@inertiajs/vue3";
4 
5const page = usePage();
6 
7const user = computed(() => page.props.auth.user);
8</script>
9 
10<template>
11 <main>
12 <header>You are logged in as: {{ user.name }}</header>
13 <article>
14 <slot />
15 </article>
16 </main>
17</template>
1<script setup>
2import { computed } from "vue";
3import { usePage } from "@inertiajs/vue3";
4 
5const page = usePage();
6 
7const user = computed(() => page.props.auth.user);
8</script>
9 
10<template>
11 <main>
12 <header>You are logged in as: {{ user.name }}</header>
13 <article>
14 <slot />
15 </article>
16 </main>
17</template>
1import { usePage } from "@inertiajs/react";
2 
3export default function Layout({ children }) {
4 const { auth } = usePage().props;
5 
6 return (
7 <main>
8 <header>You are logged in as: {auth.user.name}</header>
9 <article>{children}</article>
10 </main>
11 );
12}
1import { usePage } from "@inertiajs/react";
2 
3export default function Layout({ children }) {
4 const { auth } = usePage().props;
5 
6 return (
7 <main>
8 <header>You are logged in as: {auth.user.name}</header>
9 <article>{children}</article>
10 </main>
11 );
12}
1<script>
2 import { page } from '@inertiajs/svelte'
3</script>
4 
5<main>
6 <header>
7 You are logged in as: {$page.props.auth.user.name}
8 </header>
9 <article>
10 <slot />
11 </article>
12</main>
1<script>
2 import { page } from '@inertiajs/svelte'
3</script>
4 
5<main>
6 <header>
7 You are logged in as: {$page.props.auth.user.name}
8 </header>
9 <article>
10 <slot />
11 </article>
12</main>

TypeScript

You may configure the shared props type globally using TypeScript's declaration merging.

Flash Data

For one-time notifications like toast messages or success alerts, you may use flash data. Unlike shared data, flash data is not persisted in the browser's history state, so it won't reappear when navigating through history.