Skip to content

Commit 5b5f9b3

Browse files
authored
Merge pull request #50 from inertiajs/partial-reloads
Add support for partial reloads
2 parents 9e68492 + 120eebd commit 5b5f9b3

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

src/Response.php

Lines changed: 13 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,19 @@ public function withViewData($key, $value = null)
4648

4749
public function toResponse($request)
4850
{
51+
$only = array_filter(explode(',', $request->header('X-Inertia-Partial-Data')));
52+
53+
$props = ($only && $request->header('X-Inertia-Partial-Component') === $this->component)
54+
? array_only($this->props, $only)
55+
: $this->props;
56+
57+
$props = array_map(function ($prop) {
58+
return $prop instanceof Closure ? App::call($prop) : $prop;
59+
}, $props);
60+
4961
$page = [
5062
'component' => $this->component,
51-
'props' => $this->props,
63+
'props' => $props,
5264
'url' => $request->getRequestUri(),
5365
'version' => $this->version,
5466
];

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)