diff --git a/config/inertia.php b/config/inertia.php index 7043d0fa..0248b597 100644 --- a/config/inertia.php +++ b/config/inertia.php @@ -24,6 +24,19 @@ ], + /* + |-------------------------------------------------------------------------- + | Middleware configurtion + |-------------------------------------------------------------------------- + | + | These options configure the Inertia middleware + | + */ + + 'middleware' => [ + 'redirect_back_on_empty_response' => false, + ], + /* |-------------------------------------------------------------------------- | Testing diff --git a/src/Middleware.php b/src/Middleware.php index ad1c7658..eddfb3eb 100644 --- a/src/Middleware.php +++ b/src/Middleware.php @@ -117,7 +117,7 @@ public function handle(Request $request, Closure $next) */ public function onEmptyResponse(Request $request, Response $response): Response { - return Redirect::back(); + return config('inertia.middleware.redirect_back_on_empty_response', false) ? Redirect::back() : $response; } /** diff --git a/tests/MiddlewareTest.php b/tests/MiddlewareTest.php index abfde5d4..d6fbc065 100644 --- a/tests/MiddlewareTest.php +++ b/tests/MiddlewareTest.php @@ -4,6 +4,7 @@ use Illuminate\Http\Request; use Illuminate\Session\Middleware\StartSession; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Session; use Illuminate\Support\MessageBag; @@ -14,8 +15,9 @@ class MiddlewareTest extends TestCase { - public function test_no_response_value_by_default_means_automatically_redirecting_back_for_inertia_requests(): void + public function test_no_response_value_by_default_means_automatically_redirecting_back_for_inertia_requests_when_enabled(): void { + Config::set(['inertia.middleware.redirect_back_on_empty_response' => true]); $fooCalled = false; Route::middleware(Middleware::class)->put('/', function () use (&$fooCalled) { $fooCalled = true; @@ -33,6 +35,24 @@ public function test_no_response_value_by_default_means_automatically_redirectin $this->assertTrue($fooCalled); } + public function test_no_response_value_does_not_automatically_redirect_back(): void + { + $fooCalled = false; + Route::middleware(Middleware::class)->put('/', function () use (&$fooCalled) { + $fooCalled = true; + }); + + $response = $this + ->from('/foo') + ->put('/', [], [ + 'X-Inertia' => 'true', + 'Content-Type' => 'application/json', + ]); + + $response->assertNoContent(200); + $this->assertTrue($fooCalled); + } + public function test_no_response_value_can_be_customized_by_overriding_the_middleware_method(): void { Route::middleware(ExampleMiddleware::class)->get('/', function () {