Skip to content

Commit bdd3107

Browse files
committed
bug symfony#1976 [Fix] Handle loading="lazy" for LiveComponent only (smnandre)
This PR was merged into the 2.x branch. Discussion ---------- [Fix] Handle `loading="lazy"` for LiveComponent only | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Issues | Fix symfony#1975 | License | MIT A TwigComponent with loading=lazy was interpreted as a live and its attributes were changed. Commits ------- 0f8a530 [Fix] Handle `loading="lazy"` for LiveComponent only
2 parents 8b2afdc + 0f8a530 commit bdd3107

File tree

6 files changed

+54
-1
lines changed

6 files changed

+54
-1
lines changed

src/LiveComponent/src/EventListener/DeferLiveComponentSubscriber.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ final class DeferLiveComponentSubscriber implements EventSubscriberInterface
2929
public function onPostMount(PostMountEvent $event): void
3030
{
3131
$data = $event->getData();
32+
if (!$event->getMetadata()->get('live', false)) {
33+
// Not a live component
34+
return;
35+
}
3236

3337
if (\array_key_exists('defer', $data)) {
3438
trigger_deprecation('symfony/ux-live-component', '2.17', 'The "defer" attribute is deprecated and will be removed in 3.0. Use the "loading" attribute instead set to the value "defer".');
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component;
4+
5+
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
6+
7+
/**
8+
* @author Simon André <smn.andre@gmail.com>
9+
*/
10+
#[AsTwigComponent('simple_twig')]
11+
final class SimpleTwigComponent
12+
{
13+
public string $foo = 'foo';
14+
15+
public string $bar = 'bar';
16+
17+
public function getFooBar(): string
18+
{
19+
return $this->foo.$this->bar;
20+
}
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div{{ attributes }}>{{ this.getFooBar() }}</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<twig:simple_twig foo="Foo" bar="Bar" loading="lazy" />

src/LiveComponent/tests/Functional/EventListener/DeferLiveComponentSubscriberTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,17 @@ public function testLazyComponentIsRenderedLaterWithInitialData(): void
170170
$browser->assertElementCount('#count', 1);
171171
$browser->assertElementAttributeContains('#count', 'value', '7');
172172
}
173+
174+
public function testSubscriberDoesNotHandleTwigComponent(): void
175+
{
176+
$browser = $this->browser()
177+
->visit('/render-template/render_lazy_twig_component')
178+
->assertSuccessful();
179+
180+
$browser->assertElementCount('[loading="lazy"]', 1);
181+
$browser->assertElementCount('[data-controller]', 0);
182+
183+
$componentDiv = $browser->crawler()->filter('div');
184+
$this->assertSame('<div loading="lazy">FooBar</div>', trim($componentDiv->outerHtml()));
185+
}
173186
}

src/LiveComponent/tests/Unit/EventListener/DeferLiveComponentSubscriberTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ public function testLoadingAttributeIsExtracted()
3636
$this->assertArrayNotHasKey('loading', $event->getData());
3737
}
3838

39+
public function testLoadingAttributeIsNotExtractedWhenComponentIsNotLive()
40+
{
41+
$data = ['loading' => 'lazy'];
42+
$event = new PostMountEvent(new \stdClass(), $data, new ComponentMetadata([]));
43+
$event->setData($data);
44+
45+
$subscriber = new DeferLiveComponentSubscriber();
46+
$subscriber->onPostMount($event);
47+
48+
$this->assertArrayNotHasKey('loading', $event->getExtraMetadata());
49+
$this->assertArrayHasKey('loading', $event->getData());
50+
}
51+
3952
/**
4053
* @group legacy
4154
*/
@@ -110,7 +123,7 @@ public static function provideInvalidLoadingValues()
110123

111124
private function createPostMountEvent(array $data): PostMountEvent
112125
{
113-
$componentMetadata = new ComponentMetadata([]);
126+
$componentMetadata = new ComponentMetadata(['live' => true]);
114127
$event = new PostMountEvent(new \stdClass(), $data, $componentMetadata);
115128
$event->setData($data);
116129

0 commit comments

Comments
 (0)