Skip to content

[2.x] Added inertia:check-ssr Artisan command and isHealthy() method on HttpGateway #752

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

Merged
merged 2 commits into from
Jun 26, 2025
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
2 changes: 1 addition & 1 deletion config/inertia.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

'url' => env('INERTIA_SSR_URL', 'http://127.0.0.1:13714'),

'dispatch_without_bundle' => false,
'dispatch_without_bundle' => (bool) env('INERTIA_SSR_DISPATCH_WITHOUT_BUNDLE', false),

// 'bundle' => base_path('bootstrap/ssr/ssr.mjs'),

Expand Down
44 changes: 44 additions & 0 deletions src/Commands/CheckSsr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Inertia\Commands;

use Illuminate\Console\Command;
use Inertia\Ssr\Gateway;
use Inertia\Ssr\HasHealthCheck;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'inertia:check-ssr')]
class CheckSsr extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $signature = 'inertia:check-ssr';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Check the Inertia SSR server health status';

/**
* check the SSR server via a Node process.
*/
public function handle(Gateway $gateway): int
{
if (! $gateway instanceof HasHealthCheck) {
$this->error('The SSR gateway does not support health checks.');

return self::FAILURE;
}

($check = $gateway->isHealthy())
? $this->info('Inertia SSR server is running.')
: $this->error('Inertia SSR server is not running.');

return $check ? self::SUCCESS : self::FAILURE;
}
}
1 change: 1 addition & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ protected function registerConsoleCommands(): void
Commands\CreateMiddleware::class,
Commands\StartSsr::class,
Commands\StopSsr::class,
Commands\CheckSsr::class,
]);
}

Expand Down
11 changes: 11 additions & 0 deletions src/Ssr/HasHealthCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Inertia\Ssr;

interface HasHealthCheck
{
/**
* Determine if the SSR server is healthy.
*/
public function isHealthy(): bool;
}
21 changes: 16 additions & 5 deletions src/Ssr/HttpGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use Exception;
use Illuminate\Http\Client\StrayRequestException;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;

class HttpGateway implements Gateway
class HttpGateway implements Gateway, HasHealthCheck
{
/**
* Dispatch the Inertia page to the Server Side Rendering engine.
Expand All @@ -21,7 +22,7 @@ public function dispatch(array $page): ?Response
return null;
}

if (! $url = $this->getHttpUrl()) {
if (! $url = $this->getUrl('/render')) {
return null;
}

Expand All @@ -45,6 +46,14 @@ public function dispatch(array $page): ?Response
);
}

/**
* Determine if the SSR server is healthy.
*/
public function isHealthy(): bool
{
return Http::get($this->getUrl('/health'))->successful();
}

/**
* Determine if dispatch should proceed even if no bundle is detected.
*/
Expand All @@ -62,10 +71,12 @@ protected function bundleExists(): bool
}

/**
* Get the SSR URL from the configuration, ensuring it ends with '/render'.
* Get the SSR URL from the configuration, ensuring it ends with '/{$path}'.
*/
public function getHttpUrl(): ?string
public function getUrl(string $path): ?string
{
return str_replace('/render', '', rtrim(config('inertia.ssr.url', 'http://127.0.0.1:13714'), '/')).'/render';
$path = Str::start($path, '/');

return str_replace($path, '', rtrim(config('inertia.ssr.url', 'http://127.0.0.1:13714'), '/')).$path;
}
}
44 changes: 44 additions & 0 deletions tests/Commands/CheckSsrTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Inertia\Tests;

use Inertia\Ssr\Gateway;
use Inertia\Ssr\HttpGateway;

class CheckSsrTest extends TestCase
{
public function test_success_on_healthy_ssr_server()
{
$this->mock(HttpGateway::class, fn ($mock) => $mock
->shouldReceive('isHealthy')
->andReturnTrue()
->getMock()
);

$this->artisan('inertia:check-ssr')
->expectsOutput('Inertia SSR server is running.')
->assertExitCode(0);
}

public function test_failure_on_unhealthy_ssr_server()
{
$this->mock(HttpGateway::class, fn ($mock) => $mock
->shouldReceive('isHealthy')
->andReturnFalse()
->getMock()
);

$this->artisan('inertia:check-ssr')
->expectsOutput('Inertia SSR server is not running.')
->assertExitCode(1);
}

public function test_failure_on_unsupported_gateway()
{
$this->mock(Gateway::class);

$this->artisan('inertia:check-ssr')
->expectsOutput('The SSR gateway does not support health checks.')
->assertExitCode(1);
}
}
23 changes: 19 additions & 4 deletions tests/HttpGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ class HttpGatewayTest extends TestCase
{
protected HttpGateway $gateway;

protected string $renderUrl;

protected function setUp(): void
{
parent::setUp();

$this->gateway = new HttpGateway;
$this->renderUrl = $this->gateway->getUrl('render');

Http::preventStrayRequests();
}
Expand Down Expand Up @@ -46,7 +49,7 @@ public function test_it_uses_the_configured_http_url_when_the_bundle_file_is_det
]);

Http::fake([
$this->gateway->getHttpUrl() => Http::response(json_encode([
$this->renderUrl => Http::response(json_encode([
'head' => ['<title>SSR Test</title>', '<style></style>'],
'body' => '<div id="app">SSR Response</div>',
])),
Expand All @@ -69,7 +72,7 @@ public function test_it_uses_the_configured_http_url__when_bundle_file_detection
]);

Http::fake([
$this->gateway->getHttpUrl() => Http::response(json_encode([
$this->renderUrl => Http::response(json_encode([
'head' => ['<title>SSR Test</title>', '<style></style>'],
'body' => '<div id="app">SSR Response</div>',
])),
Expand All @@ -91,7 +94,7 @@ public function test_it_returns_null_when_the_http_request_fails()
]);

Http::fake([
$this->gateway->getHttpUrl() => Http::response(null, 500),
$this->renderUrl => Http::response(null, 500),
]);

$this->assertNull($this->gateway->dispatch(['page' => self::EXAMPLE_PAGE_OBJECT]));
Expand All @@ -105,9 +108,21 @@ public function test_it_returns_null_when_invalid_json_is_returned()
]);

Http::fake([
$this->gateway->getHttpUrl() => Http::response('invalid json'),
$this->renderUrl => Http::response('invalid json'),
]);

$this->assertNull($this->gateway->dispatch(['page' => self::EXAMPLE_PAGE_OBJECT]));
}

public function test_health_check_the_ssr_server()
{
Http::fake([
$this->gateway->getUrl('health') => Http::sequence()
->push(status: 200)
->push(status: 500),
]);

$this->assertTrue($this->gateway->isHealthy());
$this->assertFalse($this->gateway->isHealthy());
}
}