Skip to content

Commit 3ef71ed

Browse files
authored
Add inertia:start-ssr and inertia:stop-ssr artisan commands (#483)
1 parent 6806ac5 commit 3ef71ed

File tree

5 files changed

+100
-7
lines changed

5 files changed

+100
-7
lines changed

config/inertia.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
'enabled' => false,
2222

23-
'url' => 'http://127.0.0.1:13714/render',
23+
'url' => 'http://127.0.0.1:13714',
24+
25+
'bundle' => base_path('bootstrap/ssr/ssr.mjs'),
2426

2527
],
2628

src/Console/StartSsr.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Inertia\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Symfony\Component\Process\Process;
7+
8+
class StartSsr extends Command
9+
{
10+
/**
11+
* The console command name.
12+
*
13+
* @var string
14+
*/
15+
protected $name = 'inertia:start-ssr';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Start the Inertia SSR server';
23+
24+
/**
25+
* Start the SSR server via a Node process.
26+
*/
27+
public function handle()
28+
{
29+
$ssrBundle = config('inertia.ssr.bundle', base_path('bootstrap/ssr/ssr.mjs'));
30+
31+
if (! file_exists($ssrBundle)) {
32+
$this->error('Inertia SSR bundle not found: '.$ssrBundle);
33+
$this->info('Set the correct Inertia SSR bundle path in your `inertia.ssr.bundle` config.');
34+
35+
return 1;
36+
}
37+
38+
$process = new Process(['node', $ssrBundle]);
39+
$process->setTimeout(null);
40+
$process->start();
41+
42+
foreach ($process as $type => $data) {
43+
if ($process::OUT === $type) {
44+
$this->info(trim($data));
45+
} else {
46+
$this->error(trim($data));
47+
}
48+
}
49+
}
50+
}

src/Console/StopSsr.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Inertia\Console;
4+
5+
use Illuminate\Console\Command;
6+
7+
class StopSsr extends Command
8+
{
9+
/**
10+
* The console command name.
11+
*
12+
* @var string
13+
*/
14+
protected $name = 'inertia:stop-ssr';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Stop the Inertia SSR server';
22+
23+
/**
24+
* Stop the SSR server.
25+
*/
26+
public function handle()
27+
{
28+
$url = str_replace('/render', '', config('inertia.ssr.url', 'http://127.0.0.1:13714')).'/shutdown';
29+
30+
$ch = curl_init($url);
31+
curl_exec($ch);
32+
33+
if (curl_error($ch) === 'Empty reply from server') {
34+
$this->info('Inertia SSR server stopped.');
35+
} else {
36+
$this->error('Unable to connect to Inertia SSR server.');
37+
38+
return 1;
39+
}
40+
41+
curl_close($ch);
42+
}
43+
}

src/ServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ protected function registerConsoleCommands(): void
6565

6666
$this->commands([
6767
Console\CreateMiddleware::class,
68+
Console\StartSsr::class,
69+
Console\StopSsr::class,
6870
]);
6971
}
7072

src/Ssr/HttpGateway.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,20 @@
33
namespace Inertia\Ssr;
44

55
use Exception;
6-
use Illuminate\Support\Facades\Config;
76
use Illuminate\Support\Facades\Http;
87

98
class HttpGateway implements Gateway
109
{
1110
/**
1211
* Dispatch the Inertia page to the Server Side Rendering engine.
13-
*
14-
* @param array $page
15-
* @return Response|null
1612
*/
1713
public function dispatch(array $page): ?Response
1814
{
19-
if (! Config::get('inertia.ssr.enabled', false)) {
15+
if (! config('inertia.ssr.enabled', false)) {
2016
return null;
2117
}
2218

23-
$url = Config::get('inertia.ssr.url', 'http://127.0.0.1:13714/render');
19+
$url = str_replace('/render', '', config('inertia.ssr.url', 'http://127.0.0.1:13714')).'/render';
2420

2521
try {
2622
$response = Http::post($url, $page)->throw()->json();

0 commit comments

Comments
 (0)