Once Props
- Creating Once Props
- Forcing a Refresh
- Refreshing from the Client
- Expiration
- Custom Keys
- Sharing Once Props
- Conditional Once Props
- Prefetching
- Combining with Other Prop Types
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 Inertia::once()inertia::once() when returning your response. This 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]);
1use laravel::inertia::{self, Page, props};23use crate::models::Plan;45pub async fn index() -> Page {6 inertia::render("Billing", props! {7 plans => inertia::once(async || Plan::all().await),8 })9}1use laravel::inertia::{self, Page, props};23use crate::models::Plan;45pub async fn index() -> Page {6 inertia::render("Billing", props! {7 plans => inertia::once(async || Plan::all().await),8 })9}
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]);
1inertia::render("Billing", props! {2 plans => inertia::once(async || Plan::all().await).fresh(),3})1inertia::render("Billing", props! {2 plans => inertia::once(async || Plan::all().await).fresh(),3})
You may also refresh a once prop conditionally by passing a boolean to fresh()using fresh_if().
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]);
1inertia::render("Billing", props! {2 plans => inertia::once(async || Plan::all().await).fresh_if(condition),3})1inertia::render("Billing", props! {2 plans => inertia::once(async || Plan::all().await).fresh_if(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";23router.reload({ only: ["plans"] });1import { router } from "@inertiajs/vue3";23router.reload({ only: ["plans"] });
1import { router } from "@inertiajs/react";23router.reload({ only: ["plans"] });1import { router } from "@inertiajs/react";23router.reload({ only: ["plans"] });
1import { router } from "@inertiajs/svelte";23router.reload({ only: ["plans"] });1import { router } from "@inertiajs/svelte";23router.reload({ only: ["plans"] });
Expiration
You may set an expiration time using the until() method, which accepts a DateTimeInterface, DateInterval, or an integer (seconds)a time span (such as 1.day()) or a std::time::Duration. 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]);
1use laravel::inertia::{self, Page, props};2use laravel::prelude::*;34use crate::models::ExchangeRate;56pub async fn index() -> Page {7 inertia::render("Dashboard", props! {8 rates => inertia::once(async || ExchangeRate::all().await).until(1.day()),9 })10}1use laravel::inertia::{self, Page, props};2use laravel::prelude::*;34use crate::models::ExchangeRate;56pub async fn index() -> Page {7 inertia::render("Dashboard", props! {8 rates => inertia::once(async || ExchangeRate::all().await).until(1.day()),9 })10}
Custom Keys
You may assign a custom key to the prop using as()once_as(). 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]);56// 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]);56// Invite form...7return Inertia::render('Team/Invite', [8 'availableRoles' => Inertia::once(fn () => Role::all())->as('roles'),9]);
1// Team member list...2inertia::render("Team/Index", props! {3 memberRoles => inertia::once(async || Role::all().await).once_as("roles"),4})56// Invite form...7inertia::render("Team/Invite", props! {8 availableRoles => inertia::once(async || Role::all().await).once_as("roles"),9})1// Team member list...2inertia::render("Team/Index", props! {3 memberRoles => inertia::once(async || Role::all().await).once_as("roles"),4})56// Invite form...7inertia::render("Team/Invite", props! {8 availableRoles => inertia::once(async || Role::all().await).once_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 Inertia::share()HandleInertiaRequests::share or inertia::share().
1Inertia::share('countries', Inertia::once(fn () => Country::all()));1Inertia::share('countries', Inertia::once(fn () => Country::all()));
1inertia::share("countries", inertia::once(async || Country::all().await))?;1inertia::share("countries", inertia::once(async || Country::all().await))?;
You may also use the shareOnce() convenience method (which supports chaining as(), fresh(), and until()), or define a dedicated shareOnce() method in your middleware:
1Inertia::shareOnce('countries', fn () => Country::all())->until(now()->addDay());1Inertia::shareOnce('countries', fn () => Country::all())->until(now()->addDay());
1class HandleInertiaRequests extends Middleware2{3 public function shareOnce(Request $request): array4 {5 return array_merge(parent::shareOnce($request), [6 'countries' => fn () => Country::all(),7 ]);8 }9}1class HandleInertiaRequests extends Middleware2{3 public function shareOnce(Request $request): array4 {5 return array_merge(parent::shareOnce($request), [6 'countries' => fn () => Country::all(),7 ]);8 }9}
Conditional Once Props
Some shared data is mostly stable but changes at specific moments, like the authenticated user. You may return a once prop while a user is authenticated, and null otherwise. Sign in and sign out usually end with a redirect to a page that does not explicitly request the auth prop, so a remembered once value would go stale.
Returning null on signed-out requests overwrites the remembered user on every response, while returning a once prop when authenticated lets the client remember the user across visits.
1class HandleInertiaRequests extends Middleware2{3 public function share(Request $request): array4 {5 return [6 ...parent::share($request),7 'auth' => $request->user()8 ? Inertia::once(fn () => $request->user())9 : null,10 ];11 }12}1class HandleInertiaRequests extends Middleware2{3 public function share(Request $request): array4 {5 return [6 ...parent::share($request),7 'auth' => $request->user()8 ? Inertia::once(fn () => $request->user())9 : null,10 ];11 }12}
1use laravel::inertia::{self, Props, props};2use laravel::prelude::*;34use crate::models::User;56impl inertia::Middleware for HandleInertiaRequests {7 async fn share(&self, request: &Request) -> laravel::Result<Props> {8 let auth = match request.user::<User>().await? {9 Some(user) => inertia::once(async move || user),10 None => inertia::null(),11 };1213 Ok(props! { auth => auth })14 }15}1use laravel::inertia::{self, Props, props};2use laravel::prelude::*;34use crate::models::User;56impl inertia::Middleware for HandleInertiaRequests {7 async fn share(&self, request: &Request) -> laravel::Result<Props> {8 let auth = match request.user::<User>().await? {9 Some(user) => inertia::once(async move || user),10 None => inertia::null(),11 };1213 Ok(props! { auth => auth })14 }15}
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]);
1inertia::render("Dashboard", props! {2 permissions => inertia::defer(async || Permission::all().await).once(),3 activity => inertia::lazy(async move || user.recent_activity().await).merge().once(),4 categories => inertia::optional(async || Category::all().await).once(),5})1inertia::render("Dashboard", props! {2 permissions => inertia::defer(async || Permission::all().await).once(),3 activity => inertia::lazy(async move || user.recent_activity().await).merge().once(),4 categories => inertia::optional(async || Category::all().await).once(),5})