Skip to content

Commit 95898e5

Browse files
Add support for resolving props once in Inertia middleware and response
1 parent 2074ae3 commit 95898e5

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

src/Inertia.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* @method static void version(\Closure|string|null $version)
1515
* @method static string getVersion()
1616
* @method static void resolveUrlUsing(\Closure|null $urlResolver = null)
17+
* @method static void resolveOncePropsUsing(\Closure|null $urlResolver = null)
1718
* @method static \Inertia\OptionalProp optional(callable $callback)
1819
* @method static \Inertia\LazyProp lazy(callable $callback)
1920
* @method static \Inertia\DeferProp defer(callable $callback, string $group = 'default')

src/Middleware.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ public function urlResolver()
7979
return null;
8080
}
8181

82+
/**
83+
* Defines the props that are shared for one time.
84+
*
85+
* @return Closure|null
86+
*/
87+
public function oncePropsResolver()
88+
{
89+
return null;
90+
}
91+
8292
/**
8393
* Handle the incoming request.
8494
*
@@ -97,6 +107,10 @@ public function handle(Request $request, Closure $next)
97107
Inertia::resolveUrlUsing($urlResolver);
98108
}
99109

110+
if ($oncePropsResolver = $this->oncePropsResolver()) {
111+
Inertia::resolveOncePropsUsing($oncePropsResolver);
112+
}
113+
100114
$response = $next($request);
101115
$response->headers->set('Vary', Header::INERTIA);
102116

src/Response.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class Response implements Responsable
3838

3939
protected ?Closure $urlResolver = null;
4040

41+
protected ?Closure $oncePropsResolver = null;
42+
4143
/**
4244
* @param array|Arrayable $props
4345
*/
@@ -47,7 +49,8 @@ public function __construct(
4749
string $rootView = 'app',
4850
string $version = '',
4951
bool $encryptHistory = false,
50-
?Closure $urlResolver = null
52+
?Closure $urlResolver = null,
53+
?Closure $oncePropsResolver = null,
5154
) {
5255
$this->component = $component;
5356
$this->props = $props instanceof Arrayable ? $props->toArray() : $props;
@@ -56,6 +59,7 @@ public function __construct(
5659
$this->clearHistory = session()->pull('inertia.clear_history', false);
5760
$this->encryptHistory = $encryptHistory;
5861
$this->urlResolver = $urlResolver;
62+
$this->oncePropsResolver = $oncePropsResolver;
5963
}
6064

6165
/**
@@ -132,6 +136,8 @@ public function toResponse($request)
132136
return new JsonResponse($page, 200, [Header::INERTIA => 'true']);
133137
}
134138

139+
$page += $this->resolveOnceProps($request);
140+
135141
return ResponseFactory::view($this->rootView, $this->viewData + ['page' => $page]);
136142
}
137143

@@ -376,6 +382,15 @@ public function resolveDeferredProps(Request $request): array
376382
return $deferredProps->isNotEmpty() ? ['deferredProps' => $deferredProps->toArray()] : [];
377383
}
378384

385+
public function resolveOnceProps(Request $request): array
386+
{
387+
$onceProps = $this->oncePropsResolver
388+
? App::call($this->oncePropsResolver, ['request' => $request])
389+
: [];
390+
391+
return ['onceProps' => $onceProps];
392+
}
393+
379394
/**
380395
* Determine if the request is a partial request.
381396
*/

src/ResponseFactory.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ class ResponseFactory
3535
/** @var Closure|null */
3636
protected $urlResolver;
3737

38-
/***
38+
/** @var Closure|null */
39+
protected $oncePropsResolver;
40+
41+
/**
3942
* @param string $name The name of the root view
4043
* @return void
4144
*/
@@ -102,6 +105,11 @@ public function resolveUrlUsing(?Closure $urlResolver = null): void
102105
$this->urlResolver = $urlResolver;
103106
}
104107

108+
public function resolveOncePropsUsing(?Closure $oncePropsResolver = null): void
109+
{
110+
$this->oncePropsResolver = $oncePropsResolver;
111+
}
112+
105113
public function clearHistory(): void
106114
{
107115
session(['inertia.clear_history' => true]);
@@ -189,6 +197,7 @@ public function render(string $component, $props = []): Response
189197
$this->getVersion(),
190198
$this->encryptHistory ?? config('inertia.history.encrypt', false),
191199
$this->urlResolver,
200+
$this->oncePropsResolver,
192201
);
193202
}
194203

stubs/middleware.stub

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,16 @@ class {{ class }} extends Middleware
4040
//
4141
];
4242
}
43+
44+
/**
45+
* Defines the props that are shared for one time.
46+
*
47+
* @return Closure
48+
*/
49+
public function oncePropsResolver()
50+
{
51+
return function (Request $request): array {
52+
return [];
53+
};
54+
}
4355
}

0 commit comments

Comments
 (0)