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

Make automatic redirect opt-in instead of opt-out #410

Closed
Closed
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
13 changes: 13 additions & 0 deletions config/inertia.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@

],

/*
|--------------------------------------------------------------------------
| Middleware configurtion
|--------------------------------------------------------------------------
|
| These options configure the Inertia middleware
|
*/

'middleware' => [
'redirect_back_on_empty_response' => false,
],

/*
|--------------------------------------------------------------------------
| Testing
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
22 changes: 21 additions & 1 deletion tests/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 () {
Expand Down