Skip to content

Commit 0d9af3b

Browse files
authored
Flush Vite per-request state (#1016)
1 parent 5a8b439 commit 0d9af3b

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

src/Concerns/ProvidesDefaultConfigurationOptions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ public static function prepareApplicationForNextOperation(): array
4848
\Laravel\Octane\Listeners\FlushDatabaseRecordModificationState::class,
4949
\Laravel\Octane\Listeners\FlushDatabaseQueryLog::class,
5050
\Laravel\Octane\Listeners\RefreshQueryDurationHandling::class,
51-
\Laravel\Octane\Listeners\FlushLogContext::class,
5251
\Laravel\Octane\Listeners\FlushArrayCache::class,
52+
\Laravel\Octane\Listeners\FlushLogContext::class,
5353
\Laravel\Octane\Listeners\FlushMonologState::class,
5454
\Laravel\Octane\Listeners\FlushStrCache::class,
5555
\Laravel\Octane\Listeners\FlushTranslatorCache::class,
56+
\Laravel\Octane\Listeners\FlushVite::class,
5657

5758
// First-Party Packages...
5859
\Laravel\Octane\Listeners\PrepareInertiaForNextOperation::class,

src/Listeners/FlushVite.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Laravel\Octane\Listeners;
4+
5+
use Illuminate\Foundation\Vite;
6+
7+
class FlushVite
8+
{
9+
/**
10+
* Handle the event.
11+
*
12+
* @param mixed $event
13+
* @return void
14+
*/
15+
public function handle($event)
16+
{
17+
if (! $event->sandbox->resolved(Vite::class)) {
18+
return;
19+
}
20+
21+
$vite = $event->sandbox->make(Vite::class);
22+
23+
if (method_exists($vite, 'flush')) {
24+
$vite->flush();
25+
}
26+
}
27+
}

tests/Listeners/FlushViteTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Laravel\Octane\Listeners;
4+
5+
use Illuminate\Foundation\Vite;
6+
use Illuminate\Http\Request;
7+
use Laravel\Octane\Tests\TestCase;
8+
9+
class FlushViteTest extends TestCase
10+
{
11+
public function test_it_flushes_vite()
12+
{
13+
[$app, $worker] = $this->createOctaneContext([
14+
Request::create('/', 'GET'),
15+
Request::create('/', 'GET'),
16+
Request::create('/', 'GET'),
17+
]);
18+
$app['router']->get('/', fn () => 'ok');
19+
20+
$app->instance(Vite::class, $vite = new class extends Vite
21+
{
22+
public int $flushCalled = 0;
23+
24+
public function flush()
25+
{
26+
$this->flushCalled++;
27+
}
28+
});
29+
$worker->run();
30+
31+
$this->assertSame(3, $vite->flushCalled);
32+
}
33+
34+
public function test_it_handles_instances_of_vite_without_flush_method()
35+
{
36+
[$app, $worker] = $this->createOctaneContext([
37+
Request::create('/', 'GET'),
38+
Request::create('/', 'GET'),
39+
Request::create('/', 'GET'),
40+
]);
41+
$app['router']->get('/', fn () => 'ok');
42+
43+
$app->instance(Vite::class, new class
44+
{
45+
//
46+
});
47+
$worker->run();
48+
49+
$this->assertTrue(true);
50+
}
51+
}

0 commit comments

Comments
 (0)