Skip to content

Commit

Permalink
Auto enable SSR based on existence of SSR bundle (#487)
Browse files Browse the repository at this point in the history
* Auto enable SSR based on existence of SSR bundle

* Terminate SSR node process when PHP process dies

* Add missing comma
  • Loading branch information
reinink authored Jan 13, 2023
1 parent c6cfdac commit 4e5bf5c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
7 changes: 4 additions & 3 deletions config/inertia.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
| These options configures if and how Inertia uses Server Side Rendering
| to pre-render the initial visits made to your application's pages.
|
| You can specify a custom SSR bundle path, or omit it to let Inertia
| try and automatically detect it for you.
|
| Do note that enabling these options will NOT automatically make SSR work,
| as a separate rendering service needs to be available. To learn more,
| please visit https://inertiajs.com/server-side-rendering
Expand All @@ -18,11 +21,9 @@

'ssr' => [

'enabled' => false,

'url' => 'http://127.0.0.1:13714',

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

],

Expand Down
29 changes: 24 additions & 5 deletions src/Commands/StartSsr.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Inertia\Commands;

use Inertia\Ssr\SsrException;
use Illuminate\Console\Command;
use Inertia\Ssr\BundleDetector;
use Symfony\Component\Process\Process;

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

if (! file_exists($ssrBundle)) {
$this->error('Inertia SSR bundle not found: '.$ssrBundle);
$this->info('Set the correct Inertia SSR bundle path in your `inertia.ssr.bundle` config.');
if ($bundle === null) {
$this->error(
$configuredBundle
? 'Inertia SSR bundle not found at the configured path: "'.$configuredBundle.'"'
: 'Inertia SSR bundle not found. Set the correct Inertia SSR bundle path in your `inertia.ssr.bundle` config.'
);

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

$process = new Process(['node', $ssrBundle]);
$this->callSilently('inertia:stop-ssr');

$process = new Process(['node', $bundle]);
$process->setTimeout(null);
$process->start();

$stop = function () use ($process) {
$process->stop();
};
pcntl_async_signals(true);
pcntl_signal(SIGINT, $stop);
pcntl_signal(SIGQUIT, $stop);
pcntl_signal(SIGTERM, $stop);

foreach ($process as $type => $data) {
if ($process::OUT === $type) {
$this->info(trim($data));
Expand Down
18 changes: 18 additions & 0 deletions src/Ssr/BundleDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Inertia\Ssr;

class BundleDetector
{
public function detect()
{
return collect([
config('inertia.ssr.bundle'),
base_path('bootstrap/ssr/ssr.mjs'),
base_path('bootstrap/ssr/ssr.js'),
public_path('js/ssr.js'),
])->filter()->first(function ($path) {
return file_exists($path);
});
}
}
2 changes: 1 addition & 1 deletion src/Ssr/HttpGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class HttpGateway implements Gateway
*/
public function dispatch(array $page): ?Response
{
if (! config('inertia.ssr.enabled', false)) {
if (! (new BundleDetector())->detect()) {
return null;
}

Expand Down

0 comments on commit 4e5bf5c

Please sign in to comment.