|
6 | 6 | use Illuminate\Contracts\Session\Session; |
7 | 7 | use Illuminate\Http\JsonResponse; |
8 | 8 | use Illuminate\Http\Request; |
| 9 | +use Illuminate\Http\Response as LaravelResponse; |
9 | 10 | use Illuminate\Support\Facades\Blade; |
10 | 11 | use Illuminate\Support\Str; |
11 | 12 | use Illuminate\Validation\ValidationException; |
| 13 | +use Illuminate\View\View; |
12 | 14 | use ProtoneMedia\Splade\SpladeCore; |
13 | 15 | use ProtoneMedia\Splade\Ssr; |
14 | 16 | use Symfony\Component\HttpFoundation\BinaryFileResponse; |
@@ -77,40 +79,55 @@ public function handle(Request $request, Closure $next) |
77 | 79 | return $response; |
78 | 80 | } |
79 | 81 |
|
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 | + } |
101 | 85 |
|
102 | | - $viewData['ssrBody'] = $data['body'] ?? null; |
103 | | - } |
| 86 | + $originalContent = $response->getContent() ?: ''; |
| 87 | + $originalViewData = []; |
104 | 88 |
|
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 | + ]; |
108 | 103 |
|
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'], |
111 | 110 | ); |
| 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()); |
112 | 123 | } |
113 | 124 |
|
| 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 | + |
114 | 131 | return $response; |
115 | 132 | } |
116 | 133 |
|
|
0 commit comments