Skip to content

Once Props

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

Data & Props

Once Props

Some data rarely changes, is expensive to compute, or is simply large. Rather than including this data in every response, you may use once props. These props are remembered by the client and reused on subsequent pages that include the same prop. This makes them ideal for shared data.

Creating Once Props

To create a once prop, use the Inertia::once() method when returning your response. This method receives a callback that returns the prop data.

1return Inertia::render('Billing', [
2 'plans' => Inertia::once(fn () => Plan::all()),
3]);
1return Inertia::render('Billing', [
2 'plans' => Inertia::once(fn () => Plan::all()),
3]);

After the client has received this prop, subsequent requests will skip resolving the callback and exclude the prop from the response. The client only remembers once props while navigating between pages that include them.

Navigating to a page without the once prop will forget the remembered value, and it will be resolved again on the next page that has it. In practice, this is rarely an issue since once props are typically used as shared data or within a specific section of your application.

Forcing a Refresh

You may force a once prop to be refreshed using the fresh() method.

1return Inertia::render('Billing', [
2 'plans' => Inertia::once(fn () => Plan::all())->fresh(),
3]);
1return Inertia::render('Billing', [
2 'plans' => Inertia::once(fn () => Plan::all())->fresh(),
3]);

This method also accepts a boolean, allowing you to conditionally refresh the prop.

1return Inertia::render('Billing', [
2 'plans' => Inertia::once(fn () => Plan::all())->fresh($condition),
3]);
1return Inertia::render('Billing', [
2 'plans' => Inertia::once(fn () => Plan::all())->fresh($condition),
3]);

Refreshing from the Client

You may refresh a once prop from the client-side using a partial reload. The server will always resolve a once prop when explicitly requested.

1import { router } from '@inertiajs/vue3'
2 
3router.reload({ only: ['plans'] })
1import { router } from '@inertiajs/vue3'
2 
3router.reload({ only: ['plans'] })
1import { router } from '@inertiajs/react'
2 
3router.reload({ only: ['plans'] })
1import { router } from '@inertiajs/react'
2 
3router.reload({ only: ['plans'] })
1import { router } from '@inertiajs/svelte'
2 
3router.reload({ only: ['plans'] })
1import { router } from '@inertiajs/svelte'
2 
3router.reload({ only: ['plans'] })

Expiration

You may set an expiration time using the until() method. This method accepts a DateTimeInterface, DateInterval, or an integer (seconds). The prop will be refreshed on a subsequent visit after the expiration time has passed.

1return Inertia::render('Dashboard', [
2 'rates' => Inertia::once(fn () => ExchangeRate::all())->until(now()->addDay()),
3]);
1return Inertia::render('Dashboard', [
2 'rates' => Inertia::once(fn () => ExchangeRate::all())->until(now()->addDay()),
3]);

Custom Keys

You may assign a custom key to the prop using the as() method. This is useful when you want to share data across multiple pages while using different prop names.

1// Team member list...
2return Inertia::render('Team/Index', [
3 'memberRoles' => Inertia::once(fn () => Role::all())->as('roles'),
4]);
5 
6// Invite form...
7return Inertia::render('Team/Invite', [
8 'availableRoles' => Inertia::once(fn () => Role::all())->as('roles'),
9]);
1// Team member list...
2return Inertia::render('Team/Index', [
3 'memberRoles' => Inertia::once(fn () => Role::all())->as('roles'),
4]);
5 
6// Invite form...
7return Inertia::render('Team/Invite', [
8 'availableRoles' => Inertia::once(fn () => Role::all())->as('roles'),
9]);

Both pages share the same underlying data because they use the same custom key, so the prop is only resolved for whichever page you visit first.

Sharing Once Props

You may share once props globally using the Inertia::share() method.

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

Or, for convenience, you may use the shareOnce() method.

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

You may also chain as(), fresh(), and until() onto the shareOnce method.

1Inertia::shareOnce('countries', fn () => Country::all())->until(now()->addDay());
1Inertia::shareOnce('countries', fn () => Country::all())->until(now()->addDay());

Additionally, you may define a dedicated shareOnce() method in your 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}

Prefetching

Once props are compatible with prefetching. The client automatically includes any remembered once props in prefetched responses, so navigating to a prefetched page will already have the once props available.

Prefetched pages containing an expired once prop will be invalidated from the cache.

Combining with Other Prop Types

The once() modifier may be chained onto deferred, merge, and optional props.

1return Inertia::render('Dashboard', [
2 'permissions' => Inertia::defer(fn () => Permission::all())->once(),
3 'activity' => Inertia::merge(fn () => $user->recentActivity())->once(),
4 'categories' => Inertia::optional(fn () => Category::all())->once(),
5]);
1return Inertia::render('Dashboard', [
2 'permissions' => Inertia::defer(fn () => Permission::all())->once(),
3 'activity' => Inertia::merge(fn () => $user->recentActivity())->once(),
4 'categories' => Inertia::optional(fn () => Category::all())->once(),
5]);