Skip to content

[2.x] Support SSR in Vite dev mode #673

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

Open
wants to merge 6 commits into
base: 2.x
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"symfony/console": "^6.2|^7.0"
},
"require-dev": {
"guzzlehttp/guzzle": "^7.2",
"roave/security-advisories": "dev-master",
"orchestra/testbench": "^8.0|^9.2|^10.0",
"mockery/mockery": "^1.3.3",
Expand All @@ -52,4 +53,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
33 changes: 30 additions & 3 deletions src/Ssr/HttpGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Inertia\Ssr;

use Exception;
use Illuminate\Http\Client\StrayRequestException;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Vite;

class HttpGateway implements Gateway
{
Expand All @@ -12,15 +14,17 @@ class HttpGateway implements Gateway
*/
public function dispatch(array $page): ?Response
{
if (! config('inertia.ssr.enabled', true) || ! (new BundleDetector)->detect()) {
if (! config('inertia.ssr.enabled', true) || ! ($url = $this->getHttpUrl())) {
return null;
}

$url = str_replace('/render', '', rtrim(config('inertia.ssr.url', 'http://127.0.0.1:13714'), '/')).'/render';

try {
$response = Http::post($url, $page)->throw()->json();
} catch (Exception $e) {
if ($e instanceof StrayRequestException) {
throw $e;
}

return null;
}

Expand All @@ -33,4 +37,27 @@ public function dispatch(array $page): ?Response
$response['body']
);
}

/**
* Use the Vite asset URL if Vite is running in hot mode, otherwise
* return the SSR URL from the configuration if the bundle is detected.
*/
public function getHttpUrl(): ?string
{
if (Vite::isRunningHot()) {
return Vite::asset('render');
} elseif ((new BundleDetector)->detect()) {
return $this->getSsrUrl();
}

return null;
}

/**
* Get the SSR URL from the configuration, ensuring it ends with '/render'.
*/
public function getSsrUrl(): ?string
{
return str_replace('/render', '', rtrim(config('inertia.ssr.url', 'http://127.0.0.1:13714'), '/')).'/render';
}
}
2 changes: 0 additions & 2 deletions tests/DirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ class DirectiveTest extends TestCase
/**
* Example Page Objects.
*/
protected const EXAMPLE_PAGE_OBJECT = ['component' => 'Foo/Bar', 'props' => ['foo' => 'bar'], 'url' => '/test', 'version' => '', 'encryptHistory' => false, 'clearHistory' => false];

protected function setUp(): void
{
parent::setUp();
Expand Down
126 changes: 126 additions & 0 deletions tests/HttpGatewayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace Inertia\Tests;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Vite;
use Inertia\Ssr\HttpGateway;

class HttpGatewayTest extends TestCase
{
protected HttpGateway $gateway;

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

$this->gateway = new HttpGateway;

Http::preventStrayRequests();
}

public function test_it_returns_null_when_ssr_is_disabled()
{
config([
'inertia.ssr.enabled' => false,
'inertia.ssr.bundle' => __DIR__.'/Stubs/bundle.js',
]);

Vite::shouldReceive('isRunningHot')->never();

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

public function test_it_returns_null_when_no_bundle_file_is_detected_and_vite_is_not_running()
{
config([
'inertia.ssr.enabled' => true,
'inertia.ssr.bundle' => null,
]);

Vite::shouldReceive('isRunningHot')->andReturn(false);

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

public function test_it_uses_the_configured_http_url_when_the_bundle_file_is_detected()
{
config([
'inertia.ssr.enabled' => true,
'inertia.ssr.bundle' => __DIR__.'/Stubs/bundle.js',
]);

Vite::shouldReceive('isRunningHot')->andReturn(false);

Http::fake([
$this->gateway->getHttpUrl() => Http::response(json_encode([
'head' => ['<title>SSR Test</title>', '<style></style>'],
'body' => '<div id="app">SSR Response</div>',
])),
]);

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

$this->assertEquals("<title>SSR Test</title>\n<style></style>", $response->head);
$this->assertEquals('<div id="app">SSR Response</div>', $response->body);
}

public function test_it_uses_the_vite_asset_when_it_is_running_hot_even_if_a_bundle_file_is_present()
{
config([
'inertia.ssr.enabled' => true,
'inertia.ssr.bundle' => __DIR__.'/Stubs/bundle.js',
]);

Vite::shouldReceive('isRunningHot')->andReturn(true);
Vite::shouldReceive('asset')->with('render')->andReturn($viteUrl = 'http://localhost:3000/some-url');

Http::fake([
$viteUrl => Http::response(json_encode([
'head' => ['<title>SSR Test</title>', '<style></style>'],
'body' => '<div id="app">SSR Response</div>',
])),
]);

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

$this->assertEquals("<title>SSR Test</title>\n<style></style>", $response->head);
$this->assertEquals('<div id="app">SSR Response</div>', $response->body);
}

public function test_it_returns_null_when_the_http_request_fails()
{
config([
'inertia.ssr.enabled' => true,
'inertia.ssr.bundle' => __DIR__.'/Stubs/bundle.js',
]);

Vite::shouldReceive('isRunningHot')->andReturn(false);

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

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

public function test_it_returns_null_when_invalid_json_is_returned()
{
config([
'inertia.ssr.enabled' => true,
'inertia.ssr.bundle' => __DIR__.'/Stubs/bundle.js',
]);

Vite::shouldReceive('isRunningHot')->andReturn(false);

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

$this->assertNull($this->gateway->dispatch(['page' => self::EXAMPLE_PAGE_OBJECT]));
}
}
1 change: 1 addition & 0 deletions tests/Stubs/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Hello world!");
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

abstract class TestCase extends Orchestra
{
protected const EXAMPLE_PAGE_OBJECT = ['component' => 'Foo/Bar', 'props' => ['foo' => 'bar'], 'url' => '/test', 'version' => '', 'encryptHistory' => false, 'clearHistory' => false];

protected function getPackageProviders($app): array
{
return [
Expand Down