Skip to content

Commit 727a3e7

Browse files
committed
Add support for partial reloads
1 parent 9e68492 commit 727a3e7

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

src/Response.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Inertia;
44

5+
use Closure;
56
use Illuminate\Http\JsonResponse;
7+
use Illuminate\Support\Facades\App;
68
use Illuminate\Support\Facades\View;
79
use Illuminate\Contracts\Support\Responsable;
810

@@ -46,9 +48,18 @@ public function withViewData($key, $value = null)
4648

4749
public function toResponse($request)
4850
{
51+
$only = array_filter(explode(',', $request->header('X-Inertia-Only')));
52+
$props = $only ? array_only($this->props, $only) : $this->props;
53+
54+
array_walk_recursive($props, function (&$prop) {
55+
if ($prop instanceof Closure) {
56+
$prop = App::call($prop);
57+
}
58+
});
59+
4960
$page = [
5061
'component' => $this->component,
51-
'props' => $this->props,
62+
'props' => $props,
5263
'url' => $request->getRequestUri(),
5364
'version' => $this->version,
5465
];

src/ResponseFactory.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class ResponseFactory
1010
{
1111
protected $rootView = 'app';
1212
protected $sharedProps = [];
13-
protected $sharedPropsCallbacks = [];
1413
protected $version = null;
1514

1615
public function setRootView($name)
@@ -20,8 +19,8 @@ public function setRootView($name)
2019

2120
public function share($key, $value = null)
2221
{
23-
if ($key instanceof Closure) {
24-
$this->sharedPropsCallbacks[] = $key;
22+
if (is_array($key)) {
23+
$this->sharedProps = array_merge($this->sharedProps, $key);
2524
} else {
2625
Arr::set($this->sharedProps, $key, $value);
2726
}
@@ -48,18 +47,11 @@ public function getVersion()
4847

4948
public function render($component, $props = [])
5049
{
51-
$props = array_merge($this->sharedProps, $props);
52-
53-
foreach ($this->sharedPropsCallbacks as $callback) {
54-
$props = array_merge($props, App::call($callback));
55-
}
56-
57-
array_walk_recursive($props, function (&$prop) {
58-
if ($prop instanceof Closure) {
59-
$prop = App::call($prop);
60-
}
61-
});
62-
63-
return new Response($component, $props, $this->rootView, $this->getVersion());
50+
return new Response(
51+
$component,
52+
array_merge($this->sharedProps, $props),
53+
$this->rootView,
54+
$this->getVersion()
55+
);
6456
}
6557
}

0 commit comments

Comments
 (0)