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

Commit a1e26d5

Browse files
committed
2 parents 67472bd + 001642a commit a1e26d5

File tree

6 files changed

+87
-28
lines changed

6 files changed

+87
-28
lines changed

src/Components/Lazy.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ProtoneMedia\Splade\Components;
44

5+
use Illuminate\Support\Str;
56
use Illuminate\View\Component;
67
use ProtoneMedia\Splade\SpladeCore;
78

@@ -25,9 +26,14 @@ public function __construct(
2526
*/
2627
public function render()
2728
{
29+
$key = Str::random();
30+
2831
return $this->splade->isLazyRequest()
29-
? '{{ $slot }}'
30-
: view('splade::functional.lazy', [
32+
? implode([
33+
'<!--START-SPLADE-LAZY-' . $key . '-->',
34+
'{{ $slot }}',
35+
'<!--END-SPLADE-LAZY-' . $key . '-->',
36+
]) : view('splade::functional.lazy', [
3137
'name' => $this->splade->newLazyComponentKey(),
3238
]);
3339
}

src/Components/Rehydrate.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ProtoneMedia\Splade\Components;
44

5+
use Illuminate\Support\Str;
56
use Illuminate\View\Component;
67
use ProtoneMedia\Splade\SpladeCore;
78

@@ -28,9 +29,14 @@ public function __construct(
2829
*/
2930
public function render()
3031
{
32+
$key = Str::random();
33+
3134
return $this->splade->isRehydrateRequest()
32-
? '{{ $slot }}'
33-
: view('splade::functional.rehydrate', [
35+
? implode([
36+
'<!--START-SPLADE-REHYDRATE-' . $key . '-->',
37+
'{{ $slot }}',
38+
'<!--END-SPLADE-REHYDRATE-' . $key . '-->',
39+
]) : view('splade::functional.rehydrate', [
3440
'name' => $this->splade->newRehydrateComponentKey(),
3541
'on' => $this->on,
3642
]);

src/Facades/Splade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* @method static EventRedirectFactory redirectOnEvent()
2424
* @method static EventRefresh refreshOnEvent()
2525
* @method static int getLazyComponentKey()
26+
* @method static int getRehydrateComponentKey()
2627
* @method static mixed onInit($value)
2728
* @method static mixed onLazy($value)
2829
* @method static self defaultToast(callable $toastFactory):

src/Http/PrepareViewWithLazyComponents.php

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Facades\Blade;
66
use Illuminate\Support\Facades\Event;
7+
use Illuminate\Support\Str;
78
use Illuminate\View\View;
89
use ProtoneMedia\Splade\Components\SpladeComponent;
910
use ProtoneMedia\Splade\Facades\Splade;
@@ -28,18 +29,8 @@ public function registerMacro(): self
2829
// Find the lazy components within the view
2930
preg_match_all(PrepareViewWithLazyComponents::regexForTag(SpladeComponent::tag('lazy')), $view, $matches);
3031

31-
$lazyComponents = collect($matches[0] ?? []);
32-
33-
// If this is a lazy request, get the right component and render it.
34-
if (Splade::isLazyRequest()) {
35-
return Blade::render(
36-
$lazyComponents->get(Splade::getLazyComponentKey()),
37-
$this->getData()
38-
);
39-
}
40-
41-
// Otherwise, replace all lazy components with just the placeholder
42-
$lazyComponents->each(function (string $lazyComponent, $key) use ($matches, &$view) {
32+
// Replace all lazy components with just the placeholder
33+
collect($matches[0] ?? [])->each(function (string $lazyComponent, $key) use ($matches, &$view) {
4334
preg_match_all(PrepareViewWithLazyComponents::regexForTag('x-slot:placeholder'), $lazyComponent, $placeholderMatches);
4435

4536
$view = str_replace($lazyComponent, implode('', [
@@ -55,6 +46,30 @@ public function registerMacro(): self
5546
return $this;
5647
}
5748

49+
/**
50+
* Grabs the lazy-component from the rendered content and returns it.
51+
*
52+
* @param string $content
53+
* @param int $componentKey
54+
* @return string
55+
*/
56+
public static function extractComponent(string $content, int $componentKey): string
57+
{
58+
preg_match_all('/START-SPLADE-LAZY-(\w+)-->/', $content, $matches);
59+
60+
return (string) collect($matches[1] ?? [])
61+
->mapWithKeys(function (string $name, $key) use ($content) {
62+
$rehydrate = Str::between(
63+
$content,
64+
"<!--START-SPLADE-LAZY-{$name}-->",
65+
"<!--END-SPLADE-LAZY-{$name}-->"
66+
);
67+
68+
return [$key => trim($rehydrate)];
69+
})
70+
->get($componentKey);
71+
}
72+
5873
/**
5974
* Registers an event handler for the 'creating:' event, which is fired before
6075
* rendering a Blade template. This way we can, based on the request, replace
@@ -64,6 +79,11 @@ public function registerMacro(): self
6479
*/
6580
public function registerEventListener(): self
6681
{
82+
if (Splade::isLazyRequest()) {
83+
// Render normally
84+
return $this;
85+
}
86+
6787
$listener = $this->interceptCreatingViews(SpladeComponent::tag('lazy'), function (View $view) {
6888
return $view->renderWithPreparedLazyComponents();
6989
});

src/Http/PrepareViewWithRehydrateComponents.php

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Facades\Blade;
66
use Illuminate\Support\Facades\Event;
7+
use Illuminate\Support\Str;
78
use Illuminate\View\View;
89
use ProtoneMedia\Splade\Components\SpladeComponent;
910
use ProtoneMedia\Splade\Facades\Splade;
@@ -28,18 +29,8 @@ public function registerMacro(): self
2829
// Find the rehydrate components within the view
2930
preg_match_all(PrepareViewWithRehydrateComponents::regexForTag(SpladeComponent::tag('rehydrate')), $view, $matches);
3031

31-
$rehydrateComponents = collect($matches[0] ?? []);
32-
33-
// If this is a rehydrate request, get the right component and render it.
34-
if (Splade::isRehydrateRequest()) {
35-
return Blade::render(
36-
$rehydrateComponents->get(Splade::getRehydrateComponentKey()),
37-
$this->getData()
38-
);
39-
}
40-
41-
// Otherwise, extract the (optional) placeholder
42-
$rehydrateComponents->each(function (string $rehydrateComponent) use (&$view) {
32+
// Extract the (optional) placeholder
33+
collect($matches[0] ?? [])->each(function (string $rehydrateComponent) use (&$view) {
4334
preg_match_all(PrepareViewWithRehydrateComponents::regexForTag('x-slot:placeholder'), $rehydrateComponent, $placeholderMatches);
4435

4536
$placeholder = $placeholderMatches[0][0] ?? '';
@@ -60,6 +51,30 @@ public function registerMacro(): self
6051
return $this;
6152
}
6253

54+
/**
55+
* Grabs the rehydrate-component from the rendered content and returns it.
56+
*
57+
* @param string $content
58+
* @param int $componentKey
59+
* @return string
60+
*/
61+
public static function extractComponent(string $content, int $componentKey): string
62+
{
63+
preg_match_all('/START-SPLADE-REHYDRATE-(\w+)-->/', $content, $matches);
64+
65+
return (string) collect($matches[1] ?? [])
66+
->mapWithKeys(function (string $name, $key) use ($content) {
67+
$rehydrate = Str::between(
68+
$content,
69+
"<!--START-SPLADE-REHYDRATE-{$name}-->",
70+
"<!--END-SPLADE-REHYDRATE-{$name}-->"
71+
);
72+
73+
return [$key => trim($rehydrate)];
74+
})
75+
->get($componentKey);
76+
}
77+
6378
/**
6479
* Registers an event handler for the 'creating:' event, which is fired before
6580
* rendering a Blade template. This way we can, based on the request, replace
@@ -69,6 +84,11 @@ public function registerMacro(): self
6984
*/
7085
public function registerEventListener(): self
7186
{
87+
if (Splade::isRehydrateRequest()) {
88+
// Render normally
89+
return $this;
90+
}
91+
7292
$listener = $this->interceptCreatingViews(SpladeComponent::tag('rehydrate'), function (View $view) {
7393
return $view->renderWithPreparedRehydrateComponents();
7494
});

src/Http/SpladeMiddleware.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ private function handleSpladeRequest(Request $request, Response $response, objec
139139
// Extract the Dynamic Content, we'll return that separately so Vue can handle it.
140140
[$content, $dynamics] = static::extractDynamicsFromContent($content);
141141

142+
if ($this->splade->isLazyRequest()) {
143+
$content = PrepareViewWithLazyComponents::extractComponent($content, $this->splade->getLazyComponentKey()) ?: $content;
144+
} elseif ($this->splade->isRehydrateRequest()) {
145+
$content = PrepareViewWithRehydrateComponents::extractComponent($content, $this->splade->getRehydrateComponentKey());
146+
}
147+
142148
return $response->setContent(json_encode([
143149
// If this is Modal request, extract the content...
144150
'html' => $this->splade->isModalRequest() ? $this->parseModalContent($content) : $content,

0 commit comments

Comments
 (0)