Skip to content

Commit

Permalink
store history clearing in session so it also works with redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
joetannenbaum committed Oct 18, 2024
1 parent 141256b commit 402a52e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ class Response implements Responsable
/**
* @param array|Arrayable $props
*/
public function __construct(string $component, array $props, string $rootView = 'app', string $version = '', bool $clearHistory = false, bool $encryptHistory = false)
public function __construct(string $component, array $props, string $rootView = 'app', string $version = '', bool $encryptHistory = false)
{
$this->component = $component;
$this->props = $props instanceof Arrayable ? $props->toArray() : $props;
$this->rootView = $rootView;
$this->version = $version;
$this->clearHistory = $clearHistory;
$this->clearHistory = session()->pull('inertia.clear_history', false);
$this->encryptHistory = $encryptHistory;
}

Expand Down
3 changes: 1 addition & 2 deletions src/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function getVersion(): string

public function clearHistory(): void
{
$this->clearHistory = true;
session(['inertia.clear_history' => true]);
}

public function encryptHistory($encrypt = true): void
Expand Down Expand Up @@ -144,7 +144,6 @@ public function render(string $component, $props = []): Response
array_merge($this->sharedProps, $props),
$this->rootView,
$this->getVersion(),
$this->clearHistory,
$this->encryptHistory ?? config('inertia.history.encrypt', false),
);
}
Expand Down
22 changes: 22 additions & 0 deletions tests/HistoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,26 @@ public function test_the_history_can_be_cleared(): void
'clearHistory' => true,
]);
}

public function test_the_history_can_be_cleared_when_redirecting(): void
{
Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () {
Inertia::clearHistory();

return redirect('/users');
});

Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/users', function () {
return Inertia::render('User/Edit');
});

$this->followingRedirects();

$response = $this->withoutExceptionHandling()->get('/', [
'X-Inertia' => 'true',
]);

$response->assertSuccessful();
$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>');
}
}

0 comments on commit 402a52e

Please sign in to comment.