Skip to content

Commit 4e5bf5c

Browse files
authored
Auto enable SSR based on existence of SSR bundle (#487)
* Auto enable SSR based on existence of SSR bundle * Terminate SSR node process when PHP process dies * Add missing comma
1 parent c6cfdac commit 4e5bf5c

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

config/inertia.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
| These options configures if and how Inertia uses Server Side Rendering
1111
| to pre-render the initial visits made to your application's pages.
1212
|
13+
| You can specify a custom SSR bundle path, or omit it to let Inertia
14+
| try and automatically detect it for you.
15+
|
1316
| Do note that enabling these options will NOT automatically make SSR work,
1417
| as a separate rendering service needs to be available. To learn more,
1518
| please visit https://inertiajs.com/server-side-rendering
@@ -18,11 +21,9 @@
1821

1922
'ssr' => [
2023

21-
'enabled' => false,
22-
2324
'url' => 'http://127.0.0.1:13714',
2425

25-
'bundle' => base_path('bootstrap/ssr/ssr.mjs'),
26+
// 'bundle' => base_path('bootstrap/ssr/ssr.mjs'),
2627

2728
],
2829

src/Commands/StartSsr.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Inertia\Commands;
44

5+
use Inertia\Ssr\SsrException;
56
use Illuminate\Console\Command;
7+
use Inertia\Ssr\BundleDetector;
68
use Symfony\Component\Process\Process;
79

810
class StartSsr extends Command
@@ -26,19 +28,36 @@ class StartSsr extends Command
2628
*/
2729
public function handle(): int
2830
{
29-
$ssrBundle = config('inertia.ssr.bundle', base_path('bootstrap/ssr/ssr.mjs'));
31+
$bundle = (new BundleDetector())->detect();
32+
$configuredBundle = config('inertia.ssr.bundle');
3033

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+
if ($bundle === null) {
35+
$this->error(
36+
$configuredBundle
37+
? 'Inertia SSR bundle not found at the configured path: "'.$configuredBundle.'"'
38+
: 'Inertia SSR bundle not found. Set the correct Inertia SSR bundle path in your `inertia.ssr.bundle` config.'
39+
);
3440

3541
return self::FAILURE;
42+
} elseif ($configuredBundle && $bundle !== $configuredBundle) {
43+
$this->warn('Inertia SSR bundle not found at the configured path: "'.$configuredBundle.'"');
44+
$this->warn('Using a default bundle instead: "'.$bundle.'"');
3645
}
3746

38-
$process = new Process(['node', $ssrBundle]);
47+
$this->callSilently('inertia:stop-ssr');
48+
49+
$process = new Process(['node', $bundle]);
3950
$process->setTimeout(null);
4051
$process->start();
4152

53+
$stop = function () use ($process) {
54+
$process->stop();
55+
};
56+
pcntl_async_signals(true);
57+
pcntl_signal(SIGINT, $stop);
58+
pcntl_signal(SIGQUIT, $stop);
59+
pcntl_signal(SIGTERM, $stop);
60+
4261
foreach ($process as $type => $data) {
4362
if ($process::OUT === $type) {
4463
$this->info(trim($data));

src/Ssr/BundleDetector.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Inertia\Ssr;
4+
5+
class BundleDetector
6+
{
7+
public function detect()
8+
{
9+
return collect([
10+
config('inertia.ssr.bundle'),
11+
base_path('bootstrap/ssr/ssr.mjs'),
12+
base_path('bootstrap/ssr/ssr.js'),
13+
public_path('js/ssr.js'),
14+
])->filter()->first(function ($path) {
15+
return file_exists($path);
16+
});
17+
}
18+
}

src/Ssr/HttpGateway.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class HttpGateway implements Gateway
1212
*/
1313
public function dispatch(array $page): ?Response
1414
{
15-
if (! config('inertia.ssr.enabled', false)) {
15+
if (! (new BundleDetector())->detect()) {
1616
return null;
1717
}
1818

0 commit comments

Comments
 (0)