Skip to content

Commit 330dcb4

Browse files
committed
Keep only partial data in mergeProps
1 parent a1f1626 commit 330dcb4

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/Response.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,15 @@ public function resolveCacheDirections(Request $request): array
310310
public function resolveMergeProps(Request $request): array
311311
{
312312
$resetProps = collect(explode(',', $request->header(Header::RESET, '')));
313+
$onlyProps = collect(explode(',', $request->header(Header::PARTIAL_ONLY, '')))->filter();
314+
$exceptProps = collect(explode(',', $request->header(Header::PARTIAL_EXCEPT, '')));
315+
313316
$mergeProps = collect($this->props)
314317
->filter(fn ($prop) => $prop instanceof Mergeable)
315318
->filter(fn ($prop) => $prop->shouldMerge())
316-
->filter(fn ($_, $key) => ! $resetProps->contains($key));
319+
->filter(fn ($_, $key) => ! $resetProps->contains($key))
320+
->filter(fn ($_, $key) => $onlyProps->isEmpty() || $onlyProps->contains($key))
321+
->filter(fn ($_, $key) => ! $exceptProps->contains($key));
317322

318323
$deepMergeProps = $mergeProps
319324
->filter(fn ($prop) => $prop->shouldDeepMerge())

tests/ResponseTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,64 @@ public function test_server_response_with_defer_and_deep_merge_props(): void
320320
$this->assertSame('<div id="app" data-page="{&quot;component&quot;:&quot;User\/Edit&quot;,&quot;props&quot;:{&quot;user&quot;:{&quot;name&quot;:&quot;Jonathan&quot;},&quot;bar&quot;:&quot;bar value&quot;},&quot;url&quot;:&quot;\/user\/123&quot;,&quot;version&quot;:&quot;123&quot;,&quot;clearHistory&quot;:false,&quot;encryptHistory&quot;:false,&quot;deepMergeProps&quot;:[&quot;foo&quot;,&quot;bar&quot;],&quot;deferredProps&quot;:{&quot;default&quot;:[&quot;foo&quot;]}}"></div>', $view->render());
321321
}
322322

323+
public function test_exclude_merge_props_from_partial_only_response(): void
324+
{
325+
$request = Request::create('/user/123', 'GET');
326+
$request->headers->add(['X-Inertia' => 'true']);
327+
$request->headers->add(['X-Inertia-Partial-Component' => 'User/Edit']);
328+
$request->headers->add(['X-Inertia-Partial-Data' => 'user']);
329+
330+
$user = ['name' => 'Jonathan'];
331+
$response = new Response(
332+
'User/Edit',
333+
[
334+
'user' => $user,
335+
'foo' => new MergeProp('foo value'),
336+
'bar' => new MergeProp('bar value'),
337+
],
338+
'app',
339+
'123'
340+
);
341+
$response = $response->toResponse($request);
342+
$page = $response->getData();
343+
344+
$props = get_object_vars($page->props);
345+
346+
$this->assertInstanceOf(JsonResponse::class, $response);
347+
348+
$this->assertSame('Jonathan', $props['user']->name);
349+
$this->assertFalse(isset($page->mergeProps));
350+
}
351+
352+
public function test_exclude_merge_props_from_partial_except_response(): void
353+
{
354+
$request = Request::create('/user/123', 'GET');
355+
$request->headers->add(['X-Inertia' => 'true']);
356+
$request->headers->add(['X-Inertia-Partial-Component' => 'User/Edit']);
357+
$request->headers->add(['X-Inertia-Partial-Except' => 'foo']);
358+
359+
$user = ['name' => 'Jonathan'];
360+
$response = new Response(
361+
'User/Edit',
362+
[
363+
'user' => $user,
364+
'foo' => new MergeProp('foo value'),
365+
'bar' => new MergeProp('bar value'),
366+
],
367+
'app',
368+
'123'
369+
);
370+
$response = $response->toResponse($request);
371+
$page = $response->getData();
372+
373+
$props = get_object_vars($page->props);
374+
375+
$this->assertInstanceOf(JsonResponse::class, $response);
376+
377+
$this->assertSame('Jonathan', $props['user']->name);
378+
$this->assertSame(['bar'], $page->mergeProps);
379+
}
380+
323381
public function test_xhr_response(): void
324382
{
325383
$request = Request::create('/user/123', 'GET');

0 commit comments

Comments
 (0)