Skip to content

Commit cd5e6d2

Browse files
Improve Middleware compatibility (#144)
Co-authored-by: SuperDJ <[email protected]>
1 parent e473b7a commit cd5e6d2

File tree

8 files changed

+102
-5
lines changed

8 files changed

+102
-5
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
use Symfony\Component\HttpFoundation\Response;
8+
9+
class TestHttpResponseMiddleware
10+
{
11+
public function handle(Request $request, Closure $next): Response
12+
{
13+
return $next($request);
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Modal } from '@inertiaui/modal-react';
2+
3+
export default function Form() {
4+
return (
5+
<Modal>
6+
<h1>This is my modal</h1>
7+
<p>Custom middleware is compatible!</p>
8+
</Modal>
9+
);
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script setup>
2+
import { Modal } from '@inertiaui/modal-vue';
3+
</script>
4+
5+
<template>
6+
<Modal>
7+
<h1>This is my modal</h1>
8+
<p>Custom middleware is compatible!</p>
9+
</Modal>
10+
</template>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ModalLink } from '@inertiaui/modal-react';
2+
3+
export default function Index() {
4+
return (
5+
<div>
6+
<h1>Middleware Compatibility Test</h1>
7+
<p>This page tests that modals work correctly with custom middleware.</p>
8+
<br/>
9+
<ModalLink href="/middleware-compatibility/form" navigate>Open Modal</ModalLink>
10+
</div>
11+
);
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script setup>
2+
import { ModalLink } from '@inertiaui/modal-vue';
3+
</script>
4+
5+
<template>
6+
<div>
7+
<h1>Middleware Compatibility Test</h1>
8+
<p>This page tests that modals work correctly with custom middleware.</p>
9+
<br/>
10+
<ModalLink href="/middleware-compatibility/form" navigate>Open Modal</ModalLink>
11+
</div>
12+
</template>

demo-app/routes/web.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use App\Http\Middleware\TestHttpResponseMiddleware;
34
use App\Models\Role;
45
use App\Models\User;
56
use Illuminate\Support\Facades\Auth;
@@ -115,6 +116,15 @@
115116
return back();
116117
})->name('roles.store');
117118

119+
// Middleware compatibility test
120+
Route::middleware([TestHttpResponseMiddleware::class])->get('/middleware-compatibility', function () {
121+
return Inertia::render('MiddlewareCompatibility/Index');
122+
})->name('middleware-compatibility.index');
123+
124+
Route::middleware([TestHttpResponseMiddleware::class])->get('/middleware-compatibility/form', function () {
125+
return Inertia::modal('MiddlewareCompatibility/Form')->baseRoute('middleware-compatibility.index');
126+
});
127+
118128
// POST route that returns Modal
119129
Route::post('/data', function () {
120130
return inertia('Data', [
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Tests\Browser;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use Tests\DuskTestCase;
7+
8+
class MiddlewareCompatibilityTest extends DuskTestCase
9+
{
10+
#[Test]
11+
public function it_can_open_modal_with_custom_middleware_that_expects_http_response()
12+
{
13+
$this->browse(function (Browser $browser) {
14+
$browser->visit('/middleware-compatibility')
15+
->assertSee('Middleware Compatibility Test')
16+
->assertSee('This page tests that modals work correctly with custom middleware.')
17+
->click('a[href="/middleware-compatibility/form"]')
18+
->waitForModal()
19+
->assertSeeIn('.im-modal-content', 'This is my modal')
20+
->assertSeeIn('.im-modal-content', 'Custom middleware is compatible!')
21+
->clickModalCloseButton()
22+
->waitUntilMissingModal()
23+
->assertMissing('div[data-inertiaui-modal-id]');
24+
});
25+
}
26+
}

src/DispatchBaseUrlRequest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ public function __invoke(Request $originalRequest, string $baseUrl): Response
5454
->then(function ($requestForBaseUrl) use ($route) {
5555
$this->bindRequest($requestForBaseUrl);
5656

57-
return $route->run();
58-
});
57+
$response = $route->run();
58+
59+
if ($response instanceof Responsable) {
60+
return $response->toResponse($requestForBaseUrl);
61+
}
5962

60-
if ($response instanceof Responsable) {
61-
$response = $response->toResponse($requestForBaseUrl);
62-
}
63+
return $response;
64+
});
6365

6466
return tap($response, fn () => $this->bindRequest($originalRequest));
6567
}

0 commit comments

Comments
 (0)