Skip to content

[Fix] Handle loading="lazy" for LiveComponent only #1976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ final class DeferLiveComponentSubscriber implements EventSubscriberInterface
public function onPostMount(PostMountEvent $event): void
{
$data = $event->getData();
if (!$event->getMetadata()->get('live', false)) {
// Not a live component
return;
}

if (\array_key_exists('defer', $data)) {
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".');
Expand Down
21 changes: 21 additions & 0 deletions src/LiveComponent/tests/Fixtures/Component/SimpleTwigComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component;

use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

/**
* @author Simon André <smn.andre@gmail.com>
*/
#[AsTwigComponent('simple_twig')]
final class SimpleTwigComponent
{
public string $foo = 'foo';

public string $bar = 'bar';

public function getFooBar(): string
{
return $this->foo.$this->bar;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div{{ attributes }}>{{ this.getFooBar() }}</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<twig:simple_twig foo="Foo" bar="Bar" loading="lazy" />
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,17 @@ public function testLazyComponentIsRenderedLaterWithInitialData(): void
$browser->assertElementCount('#count', 1);
$browser->assertElementAttributeContains('#count', 'value', '7');
}

public function testSubscriberDoesNotHandleTwigComponent(): void
{
$browser = $this->browser()
->visit('/render-template/render_lazy_twig_component')
->assertSuccessful();

$browser->assertElementCount('[loading="lazy"]', 1);
$browser->assertElementCount('[data-controller]', 0);

$componentDiv = $browser->crawler()->filter('div');
$this->assertSame('<div loading="lazy">FooBar</div>', trim($componentDiv->outerHtml()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ public function testLoadingAttributeIsExtracted()
$this->assertArrayNotHasKey('loading', $event->getData());
}

public function testLoadingAttributeIsNotExtractedWhenComponentIsNotLive()
{
$data = ['loading' => 'lazy'];
$event = new PostMountEvent(new \stdClass(), $data, new ComponentMetadata([]));
$event->setData($data);

$subscriber = new DeferLiveComponentSubscriber();
$subscriber->onPostMount($event);

$this->assertArrayNotHasKey('loading', $event->getExtraMetadata());
$this->assertArrayHasKey('loading', $event->getData());
}

/**
* @group legacy
*/
Expand Down Expand Up @@ -110,7 +123,7 @@ public static function provideInvalidLoadingValues()

private function createPostMountEvent(array $data): PostMountEvent
{
$componentMetadata = new ComponentMetadata([]);
$componentMetadata = new ComponentMetadata(['live' => true]);
$event = new PostMountEvent(new \stdClass(), $data, $componentMetadata);
$event->setData($data);

Expand Down