-
-
Notifications
You must be signed in to change notification settings - Fork 364
Add deferred live components #1143
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\UX\LiveComponent\EventListener; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\UX\TwigComponent\Event\PostMountEvent; | ||
use Symfony\UX\TwigComponent\Event\PreRenderEvent; | ||
|
||
final class DeferLiveComponentSubscriber implements EventSubscriberInterface | ||
{ | ||
private const DEFAULT_LOADING_TAG = 'div'; | ||
|
||
private const DEFAULT_LOADING_TEMPLATE = null; | ||
|
||
public function onPostMount(PostMountEvent $event): void | ||
{ | ||
$data = $event->getData(); | ||
if (\array_key_exists('defer', $data)) { | ||
$event->addExtraMetadata('defer', true); | ||
unset($event->getData()['defer']); | ||
} | ||
|
||
if (\array_key_exists('loading-template', $data)) { | ||
$event->addExtraMetadata('loading-template', $data['loading-template']); | ||
unset($event->getData()['loading-template']); | ||
} | ||
|
||
if (\array_key_exists('loading-tag', $data)) { | ||
$event->addExtraMetadata('loading-tag', $data['loading-tag']); | ||
unset($event->getData()['loading-tag']); | ||
} | ||
|
||
$event->setData($data); | ||
} | ||
|
||
public function onPreRender(PreRenderEvent $event): void | ||
{ | ||
$mountedComponent = $event->getMountedComponent(); | ||
|
||
if (!$mountedComponent->hasExtraMetadata('defer')) { | ||
return; | ||
} | ||
|
||
$event->setTemplate('@LiveComponent/deferred.html.twig'); | ||
|
||
$variables = $event->getVariables(); | ||
$variables['loadingTemplate'] = self::DEFAULT_LOADING_TEMPLATE; | ||
$variables['loadingTag'] = self::DEFAULT_LOADING_TAG; | ||
|
||
if ($mountedComponent->hasExtraMetadata('loading-template')) { | ||
$variables['loadingTemplate'] = $mountedComponent->getExtraMetadata('loading-template'); | ||
} | ||
|
||
if ($mountedComponent->hasExtraMetadata('loading-tag')) { | ||
$variables['loadingTag'] = $mountedComponent->getExtraMetadata('loading-tag'); | ||
} | ||
|
||
$event->setVariables($variables); | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
PostMountEvent::class => ['onPostMount'], | ||
PreRenderEvent::class => ['onPreRender'], | ||
]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<{{ loadingTag }} {{ attributes }} data-action="live:connect->live#$render"> | ||
{% block loadingContent %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this working ?
If not, i thing this could be a great DX addition... wdyt ? If you'd want to use the "default" content block you'd just have to name it
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've checked this trying to do <twig:SomeLiveComponent lazy>
{% block loadingContent %}
Loading...
{% endblock %}
</twig:SomeLiveComponent> But it renders |
||
{% if loadingTemplate != null %} | ||
{{ include(loadingTemplate) }} | ||
{% endif %} | ||
{% endblock %} | ||
</{{ loadingTag }}> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component; | ||
|
||
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; | ||
use Symfony\UX\LiveComponent\DefaultActionTrait; | ||
|
||
#[AsLiveComponent('deferred_component')] | ||
final class DeferredComponent | ||
{ | ||
use DefaultActionTrait; | ||
|
||
public function getLongAwaitedData(): string | ||
{ | ||
return 'Long awaited data'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div {{ attributes }}>{{ computed.longAwaitedData }}</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
I'm loading a reaaaally slow live component |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<twig:deferred_component defer /> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<twig:deferred_component defer loading-tag='li' /> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<twig:deferred_component defer loading-template='dummy/loading.html.twig' /> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\UX\LiveComponent\Tests\Functional\EventListener; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\UX\LiveComponent\Tests\LiveComponentTestHelper; | ||
use Zenstruck\Browser\Test\HasBrowser; | ||
|
||
final class DeferLiveComponentSubscriberTest extends KernelTestCase | ||
{ | ||
use HasBrowser; | ||
use LiveComponentTestHelper; | ||
|
||
public function testItSetsDeferredTemplateIfLiveIdNotPassed(): void | ||
{ | ||
$div = $this->browser() | ||
->visit('/render-template/render_deferred_component') | ||
->assertSuccessful() | ||
->crawler() | ||
->filter('div') | ||
; | ||
|
||
$this->assertSame('', trim($div->html())); | ||
$this->assertSame('live:connect->live#$render', $div->attr('data-action')); | ||
|
||
$component = $this->mountComponent('deferred_component', [ | ||
'data-live-id' => $div->attr('data-live-id'), | ||
]); | ||
|
||
$dehydrated = $this->dehydrateComponent($component); | ||
|
||
$div = $this->browser() | ||
->visit('/_components/deferred_component?props='.urlencode(json_encode($dehydrated->getProps()))) | ||
->assertSuccessful() | ||
->crawler() | ||
->filter('div') | ||
; | ||
|
||
$this->assertSame('Long awaited data', $div->html()); | ||
} | ||
|
||
public function testItIncludesGivenTemplateWhileLoadingDeferredComponent(): void | ||
{ | ||
$div = $this->browser() | ||
->visit('/render-template/render_deferred_component_with_template') | ||
->assertSuccessful() | ||
->crawler() | ||
->filter('div') | ||
; | ||
|
||
$this->assertSame('I\'m loading a reaaaally slow live component', trim($div->html())); | ||
|
||
$component = $this->mountComponent('deferred_component', [ | ||
'data-live-id' => $div->attr('data-live-id'), | ||
]); | ||
|
||
$dehydrated = $this->dehydrateComponent($component); | ||
|
||
$div = $this->browser() | ||
->visit('/_components/deferred_component?props='.urlencode(json_encode($dehydrated->getProps()))) | ||
->assertSuccessful() | ||
->crawler() | ||
->filter('div') | ||
; | ||
|
||
$this->assertStringContainsString('Long awaited data', $div->html()); | ||
} | ||
|
||
public function testItAllowsToSetCustomLoadingHtmlTag(): void | ||
{ | ||
$crawler = $this->browser() | ||
->visit('/render-template/render_deferred_component_with_li_tag') | ||
->assertSuccessful() | ||
->crawler() | ||
; | ||
|
||
$this->assertSame(0, $crawler->filter('div')->count()); | ||
$this->assertSame(1, $crawler->filter('li')->count()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😻 This feels MUCH better to me.