Skip to content

Commit aa5d97c

Browse files
committed
Make flash data survive
1 parent f30808a commit aa5d97c

5 files changed

Lines changed: 39 additions & 4 deletions

File tree

src/Inertia.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* @method static \Inertia\ResponseFactory flash(\BackedEnum|\UnitEnum|string|array<string, mixed> $key, mixed $value = null)
3333
* @method static \Symfony\Component\HttpFoundation\RedirectResponse back(int $status = 302, array<string, string> $headers = [], mixed $fallback = false)
3434
* @method static array<string, mixed> getFlashed(\Illuminate\Http\Request|null $request = null)
35+
* @method static array<string, mixed> pullFlashed(\Illuminate\Http\Request|null $request = null)
3536
* @method static void macro(string $name, object|callable $macro)
3637
* @method static void mixin(object $mixin, bool $replace = true)
3738
* @method static bool hasMacro(string $name)

src/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ protected function resolveEncryptHistory(Request $request): array
238238
*/
239239
protected function resolveFlashData(Request $request): array
240240
{
241-
$flash = Inertia::getFlashed($request);
241+
$flash = Inertia::pullFlashed($request);
242242

243243
return $flash ? ['flash' => $flash] : [];
244244
}

src/ResponseFactory.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ public function flash(BackedEnum|UnitEnum|string|array $key, mixed $value = null
451451
$flash = [$key => $value];
452452
}
453453

454-
session()->now(SessionKey::FLASH_DATA, [
454+
session()->flash(SessionKey::FLASH_DATA, [
455455
...$this->getFlashed(),
456456
...$flash,
457457
]);
@@ -480,4 +480,16 @@ public function getFlashed(?HttpRequest $request = null): array
480480

481481
return $request->hasSession() ? $request->session()->get(SessionKey::FLASH_DATA, []) : [];
482482
}
483+
484+
/**
485+
* Retrieve and remove the flashed data from the session.
486+
*
487+
* @return array<string, mixed>
488+
*/
489+
public function pullFlashed(?HttpRequest $request = null): array
490+
{
491+
$request ??= request();
492+
493+
return $request->hasSession() ? $request->session()->pull(SessionKey::FLASH_DATA, []) : [];
494+
}
483495
}

src/ServiceProvider.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ public function boot(): void
6666
}
6767

6868
/**
69-
* Ensure redirects after PUT/PATCH/DELETE Inertia requests result
70-
* in a GET request, even when other middleware short-circuits.
69+
* Register the global redirect middleware for Inertia requests.
7170
*/
7271
protected function pushRedirectMiddleware(): void
7372
{

tests/Testing/AssertableInertiaTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,4 +484,27 @@ public function test_the_flash_data_is_available_after_redirect(): void
484484
$this->get('/action')->assertRedirect('/dashboard');
485485
$this->get('/dashboard')->assertInertia(fn (AssertableInertia $inertia) => $inertia->hasFlash('message', 'Success!'));
486486
}
487+
488+
public function test_the_flash_data_is_available_after_double_redirect(): void
489+
{
490+
$middleware = [StartSession::class, Middleware::class];
491+
492+
Route::middleware($middleware)->get('/action', function () {
493+
Inertia::flash('message', 'Success!');
494+
495+
return redirect('/intermediate');
496+
});
497+
498+
Route::middleware($middleware)->get('/intermediate', function () {
499+
return redirect('/dashboard');
500+
});
501+
502+
Route::middleware($middleware)->get('/dashboard', function () {
503+
return Inertia::render('Dashboard');
504+
});
505+
506+
$this->get('/action')->assertRedirect('/intermediate');
507+
$this->get('/intermediate')->assertRedirect('/dashboard');
508+
$this->get('/dashboard')->assertInertia(fn (AssertableInertia $inertia) => $inertia->hasFlash('message', 'Success!'));
509+
}
487510
}

0 commit comments

Comments
 (0)