Skip to content

Commit ebc3e11

Browse files
committed
Include scrollProps metadata when prop is requested to reset
1 parent de77c71 commit ebc3e11

File tree

2 files changed

+123
-4
lines changed

2 files changed

+123
-4
lines changed

src/Response.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -445,14 +445,24 @@ public function resolveCacheDirections(Request $request): array
445445
];
446446
}
447447

448+
/**
449+
* Get the props that should be reset based on the request headers.
450+
*
451+
* @return array<int, string>
452+
*/
453+
public function getResetProps(Request $request): array
454+
{
455+
return array_filter(explode(',', $request->header(Header::RESET, '')));
456+
}
457+
448458
/**
449459
* Get the props that should be considered for merging based on the request headers.
450460
*
451461
* @return \Illuminate\Support\Collection<string, \Inertia\Mergeable>
452462
*/
453-
protected function getMergePropsForRequest(Request $request): Collection
463+
protected function getMergePropsForRequest(Request $request, bool $rejectResetProps = true): Collection
454464
{
455-
$resetProps = array_filter(explode(',', $request->header(Header::RESET, '')));
465+
$resetProps = $rejectResetProps ? $this->getResetProps($request) : [];
456466
$onlyProps = array_filter(explode(',', $request->header(Header::PARTIAL_ONLY, '')));
457467
$exceptProps = array_filter(explode(',', $request->header(Header::PARTIAL_EXCEPT, '')));
458468

@@ -589,9 +599,14 @@ public function resolveDeferredProps(Request $request): array
589599
*/
590600
public function resolveScrollProps(Request $request): array
591601
{
592-
$scrollProps = $this->getMergePropsForRequest($request)
602+
$resetProps = $this->getResetProps($request);
603+
604+
$scrollProps = $this->getMergePropsForRequest($request, false)
593605
->filter(fn (Mergeable $prop) => $prop instanceof ScrollProp)
594-
->mapWithKeys(fn (ScrollProp $prop, string $key) => [$key => $prop->metadata()]);
606+
->mapWithKeys(fn (ScrollProp $prop, string $key) => [$key => [
607+
...$prop->metadata(),
608+
'reset' => in_array($key, $resetProps),
609+
]]);
595610

596611
return $scrollProps->isNotEmpty() ? ['scrollProps' => $scrollProps->toArray()] : [];
597612
}

tests/ResponseTest.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
use Inertia\LazyProp;
1818
use Inertia\MergeProp;
1919
use Inertia\ProvidesInertiaProperties;
20+
use Inertia\ProvidesScrollMetadata;
2021
use Inertia\RenderContext;
2122
use Inertia\Response;
23+
use Inertia\ScrollProp;
2224
use Inertia\Tests\Stubs\FakeResource;
2325
use Inertia\Tests\Stubs\MergeWithSharedProp;
2426
use Mockery;
27+
use PHPUnit\Framework\Attributes\DataProvider;
2528

2629
class ResponseTest extends TestCase
2730
{
@@ -138,6 +141,75 @@ public function test_server_response_with_deferred_prop_and_multiple_groups(): v
138141
$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;url&quot;:&quot;\/user\/123&quot;,&quot;version&quot;:&quot;123&quot;,&quot;clearHistory&quot;:false,&quot;encryptHistory&quot;:false,&quot;deferredProps&quot;:{&quot;default&quot;:[&quot;foo&quot;,&quot;bar&quot;],&quot;custom&quot;:[&quot;baz&quot;]}}"></div>', $view->render());
139142
}
140143

144+
public static function resetUsersProp(): array
145+
{
146+
return [
147+
'no reset' => [false],
148+
'with reset' => [true],
149+
];
150+
}
151+
152+
#[DataProvider('resetUsersProp')]
153+
public function test_server_response_with_scroll_props(bool $resetUsersProp): void
154+
{
155+
$request = Request::create('/user/123', 'GET');
156+
157+
if ($resetUsersProp) {
158+
$request->headers->add(['X-Inertia-Reset' => 'users']);
159+
}
160+
161+
$response = new Response(
162+
'User/Index',
163+
[
164+
'users' => new ScrollProp(['data' => [['id' => 1]]], 'data', new class implements ProvidesScrollMetadata
165+
{
166+
public function getPageName(): string
167+
{
168+
return 'page';
169+
}
170+
171+
public function getPreviousPage(): null
172+
{
173+
return null;
174+
}
175+
176+
public function getNextPage(): int
177+
{
178+
return 2;
179+
}
180+
181+
public function getCurrentPage(): int
182+
{
183+
return 1;
184+
}
185+
}),
186+
],
187+
'app',
188+
'123'
189+
);
190+
$response = $response->toResponse($request);
191+
/** @var BaseResponse $response */
192+
$view = $response->getOriginalContent();
193+
$page = $view->getData()['page'];
194+
195+
$this->assertInstanceOf(BaseResponse::class, $response);
196+
$this->assertInstanceOf(View::class, $view);
197+
198+
$this->assertSame('User/Index', $page['component']);
199+
$this->assertSame(['data' => [['id' => 1]]], $page['props']['users']);
200+
$this->assertSame('/user/123', $page['url']);
201+
$this->assertSame('123', $page['version']);
202+
$this->assertSame([
203+
'users' => [
204+
'pageName' => 'page',
205+
'previousPage' => null,
206+
'nextPage' => 2,
207+
'currentPage' => 1,
208+
'reset' => $resetUsersProp,
209+
],
210+
], $page['scrollProps']);
211+
}
212+
141213
public function test_server_response_with_merge_props(): void
142214
{
143215
$request = Request::create('/user/123', 'GET');
@@ -501,6 +573,38 @@ public function test_exclude_merge_props_from_partial_except_response(): void
501573
$this->assertSame(['bar'], $page->mergeProps);
502574
}
503575

576+
public function test_exclude_merge_props_when_passed_in_reset_header(): void
577+
{
578+
$request = Request::create('/user/123', 'GET');
579+
$request->headers->add(['X-Inertia' => 'true']);
580+
$request->headers->add(['X-Inertia-Partial-Component' => 'User/Edit']);
581+
$request->headers->add(['X-Inertia-Partial-Data' => 'foo']);
582+
$request->headers->add(['X-Inertia-Reset' => 'foo']);
583+
584+
$user = ['name' => 'Jonathan'];
585+
$response = new Response(
586+
'User/Edit',
587+
[
588+
'user' => $user,
589+
'foo' => new MergeProp('foo value'),
590+
'bar' => new MergeProp('bar value'),
591+
],
592+
'app',
593+
'123'
594+
);
595+
596+
/** @var JsonResponse $response */
597+
$response = $response->toResponse($request);
598+
$page = $response->getData();
599+
600+
$props = get_object_vars($page->props);
601+
602+
$this->assertInstanceOf(JsonResponse::class, $response);
603+
$this->assertSame($props['foo'], 'foo value');
604+
$this->assertArrayNotHasKey('bar', $props);
605+
$this->assertFalse(isset($page->mergeProps));
606+
}
607+
504608
public function test_xhr_response(): void
505609
{
506610
$request = Request::create('/user/123', 'GET');

0 commit comments

Comments
 (0)