Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
"v2/data-props/partial-reloads",
"v2/data-props/deferred-props",
"v2/data-props/merging-props",
"v2/data-props/once-props",
"v2/data-props/polling",
"v2/data-props/prefetching",
"v2/data-props/load-when-visible",
Expand Down
14 changes: 14 additions & 0 deletions v2/data-props/deferred-props.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ export default () => (

</CodeGroup>

## Multiple Deferred Props

If you need to wait for multiple deferred props to become available, you can specify an array to the `data` prop.

<CodeGroup>
Expand Down Expand Up @@ -173,3 +175,15 @@ export default () => (
```

</CodeGroup>

## Combining with Once Props

You may chain the `once()` modifier onto a deferred prop to ensure the data is resolved only once and remembered by the client across subsequent navigations.

```php
return Inertia::render('Dashboard', [
'stats' => Inertia::defer(fn () => Stats::generate())->once(),
]);
```

For more information on once props, see the [once props](/v2/data-props/once-props) documentation.
14 changes: 13 additions & 1 deletion v2/data-props/merging-props.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ You can also merge props directly on the client side without making a server req

## Combining with Deferred Props

You can also combine [deferred props](/v2/data-props/deferred-props) with mergeable props to defer the loading of the prop and ultimately mark it as mergeable once it's loaded.
You may combine [deferred props](/v2/data-props/deferred-props) with mergeable props to defer the loading of the prop and ultimately mark it as mergeable once it's loaded.

```php
Route::get('/users', function () {
Expand All @@ -122,6 +122,18 @@ Route::get('/users', function () {
});
```

## Combining with Once Props

You may chain the `once()` modifier onto a merge prop to ensure the data is resolved only once and remembered by the client across subsequent navigations.

```php
return Inertia::render('Users/Index', [
'activity' => Inertia::merge(fn () => $user->recentActivity())->once(),
]);
```

For more information on once props, see the [once props](/v2/data-props/once-props) documentation.

## Resetting Props

On the client side, you can indicate to the server that you would like to reset the prop. This is useful when you want to clear the prop value before merging new data, such as when the user enters a new search query on a paginated list.
Expand Down
143 changes: 143 additions & 0 deletions v2/data-props/once-props.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
---
title: 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](/v2/data-props/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.

```php
return Inertia::render('Billing', [
'plans' => Inertia::once(fn () => Plan::all()),
]);
```

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.

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

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

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

## Refreshing from the Client

You may refresh a once prop from the client-side using a [partial reload](/v2/data-props/partial-reloads). The server will always resolve a once prop when explicitly requested.

<CodeGroup>

```js Vue icon="vuejs"
import { router } from '@inertiajs/vue3'

router.reload({ only: ['plans'] })
```

```js React icon="react"
import { router } from '@inertiajs/react'

router.reload({ only: ['plans'] })
```

```js Svelte icon="s"
import { router } from '@inertiajs/svelte'

router.reload({ only: ['plans'] })
```

</CodeGroup>

## 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.

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

## 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.

```php
// Team member list...
return Inertia::render('Team/Index', [
'memberRoles' => Inertia::once(fn () => Role::all())->as('roles'),
]);

// Invite form...
return Inertia::render('Team/Invite', [
'availableRoles' => Inertia::once(fn () => Role::all())->as('roles'),
]);
```

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.

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

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

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

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

```php
Inertia::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.

```php
class HandleInertiaRequests extends Middleware
{
public function shareOnce(Request $request): array
{
return array_merge(parent::shareOnce($request), [
'countries' => fn () => Country::all(),
]);
}
}
```

## Prefetching

Once props are compatible with [prefetching](/v2/data-props/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](/v2/data-props/deferred-props), [merge](/v2/data-props/merging-props), and [optional](/v2/data-props/partial-reloads#lazy-data-evaluation) props.

```php
return Inertia::render('Dashboard', [
'permissions' => Inertia::defer(fn () => Permission::all())->once(),
'activity' => Inertia::merge(fn () => $user->recentActivity())->once(),
'categories' => Inertia::optional(fn () => Category::all())->once(),
]);
```
12 changes: 12 additions & 0 deletions v2/data-props/partial-reloads.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,15 @@ Here's a summary of each approach:
| <span style={{whiteSpace: 'nowrap'}}>`fn () => User::all()`</span> | Always | Optionally | Only when needed | |
| <span style={{whiteSpace: 'nowrap'}}>`Inertia::optional(fn () => User::all())`</span> | Never | Optionally | Only when needed | |
| <span style={{whiteSpace: 'nowrap'}}>`Inertia::always(fn () => User::all())`</span> | Always | Always | Always | |

## Combining with Once Props

You may chain the `once()` modifier onto an optional prop to ensure the data is resolved only once and remembered by the client across subsequent navigations.

```php
return Inertia::render('Users/Index', [
'users' => Inertia::optional(fn () => User::all())->once(),
]);
```

For more information on once props, see the [once props](/v2/data-props/once-props) documentation.
36 changes: 36 additions & 0 deletions v2/data-props/shared-data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,42 @@ Shared data should be used sparingly as all shared data is included with every r

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](/v2/data-props/once-props).

```php
class HandleInertiaRequests extends Middleware
{
public function share(Request $request)
{
return array_merge(parent::share($request), [
'countries' => Inertia::once(fn () => Country::all()),
]);
}
}
```

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

```php
class HandleInertiaRequests extends Middleware
{
public function shareOnce(Request $request): array
{
return array_merge(parent::shareOnce($request), [
'countries' => fn () => Country::all(),
]);
}
}
```

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

```php
Inertia::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.
Expand Down