diff --git a/src/ResponseFactory.php b/src/ResponseFactory.php index 67d0cfe8..5f735134 100644 --- a/src/ResponseFactory.php +++ b/src/ResponseFactory.php @@ -84,6 +84,10 @@ public function render($component, $props = []) public function location($url) { + if ($url instanceof RedirectResponse) { + $url = $url->getTargetUrl(); + } + if (Request::inertia()) { return BaseResponse::make('', 409, ['X-Inertia-Location' => $url]); } diff --git a/tests/ResponseFactoryTest.php b/tests/ResponseFactoryTest.php index 297a1602..1c533ef0 100644 --- a/tests/ResponseFactoryTest.php +++ b/tests/ResponseFactoryTest.php @@ -50,6 +50,30 @@ public function test_location_response_for_non_inertia_requests() $this->assertEquals('https://inertiajs.com', $response->headers->get('location')); } + public function test_location_response_for_inertia_requests_using_redirect_response() + { + Request::macro('inertia', function () { + return true; + }); + + $redirect = new RedirectResponse('https://inertiajs.com'); + $response = (new ResponseFactory())->location($redirect); + + $this->assertInstanceOf(Response::class, $response); + $this->assertEquals(409, $response->getStatusCode()); + $this->assertEquals('https://inertiajs.com', $response->headers->get('X-Inertia-Location')); + } + + public function test_location_response_for_non_inertia_requests_using_redirect_response() + { + $redirect = new RedirectResponse('https://inertiajs.com'); + $response = (new ResponseFactory())->location($redirect); + + $this->assertInstanceOf(RedirectResponse::class, $response); + $this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode()); + $this->assertEquals('https://inertiajs.com', $response->headers->get('location')); + } + public function test_the_version_can_be_a_closure() { Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () {