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 Middleware2{3 public function share(Request $request)4 {5 return array_merge(parent::share($request), [6 // Synchronously...7 'appName' => config('app.name'),89 // Lazily...10 'auth.user' => fn () => $request->user()11 ? $request->user()->only('id', 'name', 'email')12 : null,13 ]);14 }15}1class HandleInertiaRequests extends Middleware2{3 public function share(Request $request)4 {5 return array_merge(parent::share($request), [6 // Synchronously...7 'appName' => config('app.name'),89 // 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;23// Synchronously...4Inertia::share('appName', config('app.name'));56// Lazily...7Inertia::share('user', fn (Request $request) => $request->user()8 ? $request->user()->only('id', 'name', 'email')9 : null10);1use Inertia\Inertia;23// Synchronously...4Inertia::share('appName', config('app.name'));56// Lazily...7Inertia::share('user', fn (Request $request) => $request->user()8 ? $request->user()->only('id', 'name', 'email')9 : null10);
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.
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<template>2 <main>3 <header>4 You are logged in as: {{ user.name }}5 </header>6 <article>7 <slot />8 </article>9 </main>10</template>1112<script>13export default {14 computed: {15 user() {16 return this.$page.props.auth.user17 }18 }19}20</script>1<template>2 <main>3 <header>4 You are logged in as: {{ user.name }}5 </header>6 <article>7 <slot />8 </article>9 </main>10</template>1112<script>13export default {14 computed: {15 user() {16 return this.$page.props.auth.user17 }18 }19}20</script>
1<script setup>2import { computed } from 'vue'3import { usePage } from '@inertiajs/vue3'45const page = usePage()67const user = computed(() => page.props.auth.user)8</script>910<template>11 <main>12 <header>13 You are logged in as: {{ user.name }}14 </header>15 <article>16 <slot />17 </article>18 </main>19</template>1<script setup>2import { computed } from 'vue'3import { usePage } from '@inertiajs/vue3'45const page = usePage()67const user = computed(() => page.props.auth.user)8</script>910<template>11 <main>12 <header>13 You are logged in as: {{ user.name }}14 </header>15 <article>16 <slot />17 </article>18 </main>19</template>
1import { usePage } from '@inertiajs/react'23export default function Layout({ children }) {4 const { auth } = usePage().props56 return (7 <main>8 <header>9 You are logged in as: {auth.user.name}10 </header>11 <article>12 {children}13 </article>14 </main>15 )16}1import { usePage } from '@inertiajs/react'23export default function Layout({ children }) {4 const { auth } = usePage().props56 return (7 <main>8 <header>9 You are logged in as: {auth.user.name}10 </header>11 <article>12 {children}13 </article>14 </main>15 )16}
1<script>2 import { page } from '@inertiajs/svelte'3</script>45<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>45<main>6 <header>7 You are logged in as: {$page.props.auth.user.name}8 </header>9 <article>10 <slot />11 </article>12</main>
Flash Messages
Another great use-case for shared data is flash messages. These are messages stored in the session only for the next request. For example, it's common to set a flash message after completing a task and before redirecting to a different page.
Here's a simple way to implement flash messages in your Inertia applications. First, share the flash message on each request.
1class HandleInertiaRequests extends Middleware2{3 public function share(Request $request)4 {5 return array_merge(parent::share($request), [6 'flash' => [7 'message' => fn () => $request->session()->get('message')8 ],9 ]);10 }11}1class HandleInertiaRequests extends Middleware2{3 public function share(Request $request)4 {5 return array_merge(parent::share($request), [6 'flash' => [7 'message' => fn () => $request->session()->get('message')8 ],9 ]);10 }11}
Next, display the flash message in a frontend component, such as the site layout.
1<template>2 <main>3 <header></header>4 <article>5 <div v-if="$page.props.flash.message" class="alert">6 {{ $page.props.flash.message }}7 </div>8 <slot />9 </article>10 <footer></footer>11 </main>12</template>1<template>2 <main>3 <header></header>4 <article>5 <div v-if="$page.props.flash.message" class="alert">6 {{ $page.props.flash.message }}7 </div>8 <slot />9 </article>10 <footer></footer>11 </main>12</template>
1<template>2 <main>3 <header></header>4 <article>5 <div v-if="$page.props.flash.message" class="alert">6 {{ $page.props.flash.message }}7 </div>8 <slot />9 </article>10 <footer></footer>11 </main>12</template>1<template>2 <main>3 <header></header>4 <article>5 <div v-if="$page.props.flash.message" class="alert">6 {{ $page.props.flash.message }}7 </div>8 <slot />9 </article>10 <footer></footer>11 </main>12</template>
1import { usePage } from '@inertiajs/react'23export default function Layout({ children }) {4 const { flash } = usePage().props56 return (7 <main>8 <header></header>9 <article>10 {flash.message && (11 <div class="alert">{flash.message}</div>12 )}13 {children}14 </article>15 <footer></footer>16 </main>17 )18}1import { usePage } from '@inertiajs/react'23export default function Layout({ children }) {4 const { flash } = usePage().props56 return (7 <main>8 <header></header>9 <article>10 {flash.message && (11 <div class="alert">{flash.message}</div>12 )}13 {children}14 </article>15 <footer></footer>16 </main>17 )18}
1<script>2 import { page } from '@inertiajs/svelte'3</script>45<main>6 <header></header>7 <article>8 {#if $page.props.flash.message}9 <div class="alert">{$page.props.flash.message}</div>10 {/if}11 <slot />12 </article>13 <footer></footer>14</main>1<script>2 import { page } from '@inertiajs/svelte'3</script>45<main>6 <header></header>7 <article>8 {#if $page.props.flash.message}9 <div class="alert">{$page.props.flash.message}</div>10 {/if}11 <slot />12 </article>13 <footer></footer>14</main>