Skip to content
This repository was archived by the owner on Oct 20, 2025. It is now read-only.

Commit 55911a4

Browse files
authored
Update SpladeMiddleware.php (#54)
1 parent 1758e5a commit 55911a4

File tree

1 file changed

+45
-28
lines changed

1 file changed

+45
-28
lines changed

src/Http/SpladeMiddleware.php

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
use Illuminate\Contracts\Session\Session;
77
use Illuminate\Http\JsonResponse;
88
use Illuminate\Http\Request;
9+
use Illuminate\Http\Response as LaravelResponse;
910
use Illuminate\Support\Facades\Blade;
1011
use Illuminate\Support\Str;
1112
use Illuminate\Validation\ValidationException;
13+
use Illuminate\View\View;
1214
use ProtoneMedia\Splade\SpladeCore;
1315
use ProtoneMedia\Splade\Ssr;
1416
use Symfony\Component\HttpFoundation\BinaryFileResponse;
@@ -77,40 +79,55 @@ public function handle(Request $request, Closure $next)
7779
return $response;
7880
}
7981

80-
if ($response->isSuccessful()) {
81-
$originalContent = $response->getContent() ?: '';
82-
83-
[$content, $dynamics] = static::extractDynamicsFromContent($originalContent);
84-
85-
$viewData = [
86-
'components' => static::renderedComponents(),
87-
'html' => $content,
88-
'dynamics' => $dynamics,
89-
'splade' => $spladeData,
90-
'ssrHead' => null,
91-
'ssrBody' => null,
92-
];
93-
94-
if (config('splade.ssr.enabled')) {
95-
$data = $this->ssr->render(
96-
$viewData['components'],
97-
$viewData['html'],
98-
$viewData['dynamics'],
99-
$viewData['splade'],
100-
);
82+
if (!$response->isSuccessful()) {
83+
return $response;
84+
}
10185

102-
$viewData['ssrBody'] = $data['body'] ?? null;
103-
}
86+
$originalContent = $response->getContent() ?: '';
87+
$originalViewData = [];
10488

105-
if (!$viewData['ssrBody'] && config('splade.ssr.blade_fallback')) {
106-
$viewData['ssrBody'] = $originalContent;
107-
}
89+
if ($response instanceof LaravelResponse && $response->getOriginalContent() instanceof View) {
90+
$originalViewData = $response->getOriginalContent()->getData();
91+
}
92+
93+
[$content, $dynamics] = static::extractDynamicsFromContent($originalContent);
94+
95+
$viewData = [
96+
'components' => static::renderedComponents(),
97+
'html' => $content,
98+
'dynamics' => $dynamics,
99+
'splade' => $spladeData,
100+
'ssrHead' => null,
101+
'ssrBody' => null,
102+
];
108103

109-
return $response->setContent(
110-
view($this->splade->getRootView(), $viewData)->render()
104+
if (config('splade.ssr.enabled')) {
105+
$data = $this->ssr->render(
106+
$viewData['components'],
107+
$viewData['html'],
108+
$viewData['dynamics'],
109+
$viewData['splade'],
111110
);
111+
112+
$viewData['ssrBody'] = $data['body'] ?? null;
113+
}
114+
115+
if (!$viewData['ssrBody'] && config('splade.ssr.blade_fallback')) {
116+
$viewData['ssrBody'] = $originalContent;
117+
}
118+
119+
$wrappedView = view($this->splade->getRootView(), $viewData);
120+
121+
if (!$response instanceof LaravelResponse) {
122+
return $response->setContent($wrappedView->render());
112123
}
113124

125+
// this will call 'render' on the wrapped view
126+
$response->setContent($wrappedView);
127+
128+
// restore the original data (for test assertions)
129+
$wrappedView->with($originalViewData);
130+
114131
return $response;
115132
}
116133

0 commit comments

Comments
 (0)