Skip to content
This repository was archived by the owner on Oct 20, 2025. It is now read-only.
Merged
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
73 changes: 45 additions & 28 deletions src/Http/SpladeMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
use Illuminate\Contracts\Session\Session;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response as LaravelResponse;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Illuminate\View\View;
use ProtoneMedia\Splade\SpladeCore;
use ProtoneMedia\Splade\Ssr;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
Expand Down Expand Up @@ -77,40 +79,55 @@ public function handle(Request $request, Closure $next)
return $response;
}

if ($response->isSuccessful()) {
$originalContent = $response->getContent() ?: '';

[$content, $dynamics] = static::extractDynamicsFromContent($originalContent);

$viewData = [
'components' => static::renderedComponents(),
'html' => $content,
'dynamics' => $dynamics,
'splade' => $spladeData,
'ssrHead' => null,
'ssrBody' => null,
];

if (config('splade.ssr.enabled')) {
$data = $this->ssr->render(
$viewData['components'],
$viewData['html'],
$viewData['dynamics'],
$viewData['splade'],
);
if (!$response->isSuccessful()) {
return $response;
}

$viewData['ssrBody'] = $data['body'] ?? null;
}
$originalContent = $response->getContent() ?: '';
$originalViewData = [];

if (!$viewData['ssrBody'] && config('splade.ssr.blade_fallback')) {
$viewData['ssrBody'] = $originalContent;
}
if ($response instanceof LaravelResponse && $response->getOriginalContent() instanceof View) {
$originalViewData = $response->getOriginalContent()->getData();
}

[$content, $dynamics] = static::extractDynamicsFromContent($originalContent);

$viewData = [
'components' => static::renderedComponents(),
'html' => $content,
'dynamics' => $dynamics,
'splade' => $spladeData,
'ssrHead' => null,
'ssrBody' => null,
];

return $response->setContent(
view($this->splade->getRootView(), $viewData)->render()
if (config('splade.ssr.enabled')) {
$data = $this->ssr->render(
$viewData['components'],
$viewData['html'],
$viewData['dynamics'],
$viewData['splade'],
);

$viewData['ssrBody'] = $data['body'] ?? null;
}

if (!$viewData['ssrBody'] && config('splade.ssr.blade_fallback')) {
$viewData['ssrBody'] = $originalContent;
}

$wrappedView = view($this->splade->getRootView(), $viewData);

if (!$response instanceof LaravelResponse) {
return $response->setContent($wrappedView->render());
}

// this will call 'render' on the wrapped view
$response->setContent($wrappedView);

// restore the original data (for test assertions)
$wrappedView->with($originalViewData);

return $response;
}

Expand Down