Skip to content

Commit a836013

Browse files
Merge pull request #677 from inertiajs/encryption-redirect-test
[2.x] Fix clearing history when redirecting
2 parents 141256b + 402a52e commit a836013

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/Response.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ class Response implements Responsable
4141
/**
4242
* @param array|Arrayable $props
4343
*/
44-
public function __construct(string $component, array $props, string $rootView = 'app', string $version = '', bool $clearHistory = false, bool $encryptHistory = false)
44+
public function __construct(string $component, array $props, string $rootView = 'app', string $version = '', bool $encryptHistory = false)
4545
{
4646
$this->component = $component;
4747
$this->props = $props instanceof Arrayable ? $props->toArray() : $props;
4848
$this->rootView = $rootView;
4949
$this->version = $version;
50-
$this->clearHistory = $clearHistory;
50+
$this->clearHistory = session()->pull('inertia.clear_history', false);
5151
$this->encryptHistory = $encryptHistory;
5252
}
5353

src/ResponseFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function getVersion(): string
8888

8989
public function clearHistory(): void
9090
{
91-
$this->clearHistory = true;
91+
session(['inertia.clear_history' => true]);
9292
}
9393

9494
public function encryptHistory($encrypt = true): void
@@ -144,7 +144,6 @@ public function render(string $component, $props = []): Response
144144
array_merge($this->sharedProps, $props),
145145
$this->rootView,
146146
$this->getVersion(),
147-
$this->clearHistory,
148147
$this->encryptHistory ?? config('inertia.history.encrypt', false),
149148
);
150149
}

tests/HistoryTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,26 @@ public function test_the_history_can_be_cleared(): void
140140
'clearHistory' => true,
141141
]);
142142
}
143+
144+
public function test_the_history_can_be_cleared_when_redirecting(): void
145+
{
146+
Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () {
147+
Inertia::clearHistory();
148+
149+
return redirect('/users');
150+
});
151+
152+
Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/users', function () {
153+
return Inertia::render('User/Edit');
154+
});
155+
156+
$this->followingRedirects();
157+
158+
$response = $this->withoutExceptionHandling()->get('/', [
159+
'X-Inertia' => 'true',
160+
]);
161+
162+
$response->assertSuccessful();
163+
$response->assertContent('<div id="app" data-page="{&quot;component&quot;:&quot;User\/Edit&quot;,&quot;props&quot;:{&quot;errors&quot;:{}},&quot;url&quot;:&quot;\/users&quot;,&quot;version&quot;:&quot;&quot;,&quot;clearHistory&quot;:true,&quot;encryptHistory&quot;:false}"></div>');
164+
}
143165
}

0 commit comments

Comments
 (0)