Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: handle non-inertia location redirects #312

Merged
merged 1 commit into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Closure;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Response as BaseResponse;
use Illuminate\Support\Traits\Macroable;

Expand Down Expand Up @@ -82,6 +84,10 @@ public function render($component, $props = [])

public function location($url)
{
return BaseResponse::make('', 409, ['X-Inertia-Location' => $url]);
if (Request::inertia()) {
return BaseResponse::make('', 409, ['X-Inertia-Location' => $url]);
}

return new RedirectResponse($url);
}
}
23 changes: 21 additions & 2 deletions tests/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Inertia\Tests;

use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Response;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
use Inertia\LazyProp;
Expand All @@ -22,15 +24,32 @@ public function test_can_macro()
$this->assertEquals('bar', $factory->foo());
}

public function test_location_response()
public function test_location_response_for_inertia_requests()
{
Request::macro('inertia', function () {
return true;
});

$response = (new ResponseFactory())->location('https://inertiajs.com');

$this->assertInstanceOf(Response::class, $response);
$this->assertEquals(409, $response->getStatusCode());
$this->assertEquals(Response::HTTP_CONFLICT, $response->getStatusCode());
$this->assertEquals('https://inertiajs.com', $response->headers->get('X-Inertia-Location'));
}

public function test_location_response_for_non_inertia_requests()
{
Request::macro('inertia', function () {
return false;
});

$response = (new ResponseFactory())->location('https://inertiajs.com');

$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 () {
Expand Down