Files
ntspi-app/app/Ship/Ssr/SafeSsrGateway.php
F4ilji dbac7989a7 fix: department page SSR crash rendering empty body on hard reload
- Remove this. prefix from template expression in Departments/Show.vue
  (ssrRender this is undefined in strict ESM)
- Register helpers mixin in ssr.js for parity with app.js
- SafeSsrGateway: fallback to client-side render when SSR returns
  empty body or server unavailable
- Jenkins: start SSR server in background, add health check
2026-10-22 12:33:20 +05:00

87 lines
1.9 KiB
PHP

<?php
namespace App\Ship\Ssr;
use Exception;
use Illuminate\Http\Client\StrayRequestException;
use Illuminate\Support\Facades\Http;
use Inertia\Ssr\HttpGateway;
use Inertia\Ssr\Response;
class SafeSsrGateway extends HttpGateway
{
private bool $serverAvailable = false;
private bool $checked = false;
public function dispatch(array $page): ?Response
{
if (! $this->shouldDispatch()) {
return null;
}
if (! $this->isServerAvailable()) {
return null;
}
try {
$response = Http::timeout(5)
->retry(1, 200)
->post($this->getUrl('/render'), $page)
->throw()
->json();
} catch (Exception $e) {
if ($e instanceof StrayRequestException) {
throw $e;
}
$this->serverAvailable = false;
return null;
}
if (is_null($response)) {
return null;
}
$body = $response['body'] ?? '';
$head = $response['head'] ?? [];
if (empty($body) && empty($head)) {
return null;
}
return new Response(
implode("\n", $head),
$body
);
}
public function isHealthy(): bool
{
return $this->isServerAvailable();
}
private function isServerAvailable(): bool
{
if ($this->checked) {
return $this->serverAvailable;
}
$this->checked = true;
try {
$this->serverAvailable = Http::timeout(1)
->get($this->getUrl('/health'))
->successful();
} catch (Exception $e) {
if ($e instanceof StrayRequestException) {
throw $e;
}
$this->serverAvailable = false;
}
return $this->serverAvailable;
}
}