hotwired-laravel / turbo-laravel

This package gives you a set of conventions to make the most out of Hotwire in Laravel.
https://turbo-laravel.com
MIT License
803 stars 50 forks source link

Broadcasting Turbo Streams with multiple targets misses the targets attribute #102

Closed davidgut closed 1 year ago

davidgut commented 1 year ago

Hi,

When attempting to broadcast a Turbo Stream with multiple targets, the generated Turbo Stream misses the targets="..." attribute.

$feed->broadcastUpdateTo($feed->owner)
    ->targets('.notifications-ping')
    ->partial('sidebar._notifications_ping')
    ->later();

The code above creates the following Turbo stream:

<turbo-stream target="" action="update">

Expected result:

<turbo-stream targets=".notifications-ping" action="update">

I've looked into the underlying code but can't see where it "goes missing".

Running PHP 8.1.2, Laravel 9.0 and hotwired/turbo-laravel 1.10.

tonysm commented 1 year ago

Hey, interesting. Taking a look at it now.

tonysm commented 1 year ago

I've tried rendering the pending broadcast right away and that seems to work:

> $chirp->broadcastAppend()->targets('.chirps-list')->render()
= Illuminate\Support\HtmlString {#4814
    html: """
      <turbo-stream targets=".chirps-list" action="append">\n
          <template><turbo-frame\n
          id="chirp_6"\n

Same with using broadcastAppendTo():

> $chirp->broadcastAppendTo(User::first())->targets('.chirps-list')->render()
= Illuminate\Support\HtmlString {#4818
    html: """
      <turbo-stream targets=".chirps-list" action="append">\n
          <template><turbo-frame\n

Taking a look at the broadcast job.

tonysm commented 1 year ago

I tried instantiating the job manually and rendering the event it generates, and it works:

> $pending = $chirp->broadcastAppendTo(User::first())->targets('.chirps-list')->later()
> $job = new Tonysm\TurboLaravel\Jobs\BroadcastAction($pending->channels, $pending->action, $pending->target, $pending->targets, $pending->partialView, $pending->partialData, $pending->inlineContent, $pending->escapeInlineContent, $pending->attributes, null);
> $job->asEvent()->render()
= """
  <turbo-stream targets=".chirps-list" action="append">\n
tonysm commented 1 year ago

@davidgut Can you try again after running php artisan view:clear? Did you upgrade the package recently? It may be that you upgraded the package and the component views were already cached.

davidgut commented 1 year ago

The issue was I had an old turbo-frame blade file in my views/vendor/turbo-laravel folder, namely views/vendor/turbo-laravel/turbo-stream.blade.php, from an early version of this package. What solved the issue was deleting this folder.

Thank you.