Skip to content
Merged
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
31 changes: 18 additions & 13 deletions src/View/Components/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,32 @@

namespace Inertia\View\Components;

use Closure;
use Illuminate\View\Component;
use Inertia\Ssr\Response;
use Inertia\Ssr\SsrState;

class App extends Component
{
public ?Response $response;

public string $pageJson;

public function __construct(
public string $id = 'app',
) {}
) {
$state = app(SsrState::class);
$this->response = $state->dispatch();
$this->pageJson = json_encode($state->page);
}

public function render(): Closure
public function render(): string
{
return function () {
$state = app(SsrState::class);
$response = $state->dispatch();

if ($response) {
return $response->body;
}

return '<script data-page="'.$this->id.'" type="application/json">'.json_encode($state->page).'</script><div id="'.$this->id.'"></div>';
};
return <<<'blade'
@if($response)
{!! $response->body !!}
@else
<script data-page="{{ $id }}" type="application/json">{!! $pageJson !!}</script><div id="{{ $id }}"></div>
@endif
blade;
}
}
25 changes: 15 additions & 10 deletions src/View/Components/Head.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@

namespace Inertia\View\Components;

use Closure;
use Illuminate\View\Component;
use Inertia\Ssr\Response;
use Inertia\Ssr\SsrState;

class Head extends Component
{
public function render(): Closure
{
return function (array $data) {
$response = app(SsrState::class)->dispatch();
public ?Response $response;

if ($response) {
return $response->head;
}
public function __construct()
{
$this->response = app(SsrState::class)->dispatch();
}

return (string) $data['slot'];
};
public function render(): string
{
return <<<'blade'
@if($response)
{!! $response->head !!}
@else
{!! $slot !!}
@endif
blade;
}
}
16 changes: 16 additions & 0 deletions tests/ComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ public function test_head_component_without_slot_matches_directive_output_when_s
$this->assertSame($directive, $component);
}

public function test_components_do_not_create_cached_view_files_per_request(): void
{
Config::set(['inertia.ssr.enabled' => true]);

$viewCachePath = $this->app['config']['view.compiled'];
$view = '<x-inertia::head><title>Fallback</title></x-inertia::head><x-inertia::app />';

$this->renderView($view, ['page' => self::EXAMPLE_PAGE_OBJECT]);
$cachedViews = glob($viewCachePath.'/*.php');

$this->app->forgetScopedInstances();

$this->renderView($view, ['page' => ['component' => 'Different', 'props' => ['foo' => 'bar']]]);
$this->assertSame($cachedViews, glob($viewCachePath.'/*.php'));
}

public function test_ssr_state_is_scoped_and_does_not_leak_between_requests(): void
{
Config::set(['inertia.ssr.enabled' => true]);
Expand Down
Loading