Skip to content

[Turbo] Simplify stream negotiation #217

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
Jan 12, 2022
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
24 changes: 24 additions & 0 deletions src/Turbo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
# CHANGELOG

## 2.1

- `TurboStreamResponse` and `AddTurboStreamFormatSubscriber` have been removed, use native content negotiation instead:

```php
use Symfony\UX\Turbo\TurboBundle;

class TaskController extends AbstractController
{
public function new(Request $request): Response
{
// ...
if (TurboBundle::STREAM_FORMAT === $request->getPreferredFormat()) {
$request->setRequestFormat(TurboBundle::STREAM_FORMAT);
$response = $this->render('task/success.stream.html.twig', ['task' => $task]);
} else {
$response = $this->render('task/success.html.twig', ['task' => $task]);
}

return $response->setVary('Accept');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
}
```

## 2.0

- Support for `stimulus` version 2 was removed and support for `@hotwired/stimulus`
Expand Down
9 changes: 5 additions & 4 deletions src/Turbo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\UX\Turbo\Stream\TurboStreamResponse;
use Symfony\UX\Turbo\TurboBundle;
use App\Entity\Task;

class TaskController extends AbstractController
Expand All @@ -311,9 +311,10 @@ class TaskController extends AbstractController
// ... perform some action, such as saving the task to the database

// 🔥 The magic happens here! 🔥
if (TurboStreamResponse::STREAM_FORMAT === $request->getPreferredFormat()) {
// If the request comes from Turbo, only send the HTML to update using a TurboStreamResponse
return $this->render('task/success.stream.html.twig', ['task' => $task], new TurboStreamResponse());
if (TurboBundle::STREAM_FORMAT === $request->getPreferredFormat()) {
// If the request comes from Turbo, set the content type as text/vnd.turbo-stream.html and only send the HTML to update
$request->setFormat(TurboBundle::STREAM_FORMAT);
return $this->render('task/success.stream.html.twig', ['task' => $task]);
}

// If the client doesn't support JavaScript, or isn't using Turbo, the form still works as usual.
Expand Down
4 changes: 0 additions & 4 deletions src/Turbo/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Symfony\UX\Turbo\Broadcaster\ImuxBroadcaster;
use Symfony\UX\Turbo\Broadcaster\TwigBroadcaster;
use Symfony\UX\Turbo\Doctrine\BroadcastListener;
use Symfony\UX\Turbo\Stream\AddTurboStreamFormatSubscriber;
use Symfony\UX\Turbo\Twig\TwigExtension;

/*
Expand All @@ -25,9 +24,6 @@
return static function (ContainerConfigurator $container): void {
$container->services()

->set('turbo.kernel.event_subscriber', AddTurboStreamFormatSubscriber::class)
->tag('kernel.event_subscriber')

->set('turbo.broadcaster.imux', ImuxBroadcaster::class)
->args([tagged_iterator('turbo.broadcaster')])

Expand Down
67 changes: 0 additions & 67 deletions src/Turbo/Stream/AddTurboStreamFormatSubscriber.php

This file was deleted.

27 changes: 0 additions & 27 deletions src/Turbo/Stream/TurboStreamResponse.php

This file was deleted.

10 changes: 6 additions & 4 deletions src/Turbo/Tests/app/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
use Symfony\Component\Mercure\HubInterface;
use Symfony\Component\Mercure\Update;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Symfony\UX\Turbo\Stream\TurboStreamResponse;
use Symfony\UX\Turbo\TurboBundle;
use Symfony\WebpackEncoreBundle\WebpackEncoreBundle;
use Twig\Environment;
Expand Down Expand Up @@ -131,11 +130,14 @@ public function getProjectDir(): string

public function form(Request $request, Environment $twig): Response
{
if (TurboStreamResponse::STREAM_FORMAT === $request->getPreferredFormat()) {
return new TurboStreamResponse($twig->render('form.stream.html.twig'));
if (TurboBundle::STREAM_FORMAT === $request->getPreferredFormat()) {
$request->setRequestFormat(TurboBundle::STREAM_FORMAT);
$response = (new Response($twig->render('form.stream.html.twig')));
} else {
$response = new Response('Turbo not installed, default to plain HTML.');
}

return new Response('Turbo not installed, default to plain HTML.');
return $response->setVary('Accept');
}

public function chat(Request $request, FormFactoryInterface $formFactory, HubInterface $mercureHub, Environment $twig): Response
Expand Down
9 changes: 9 additions & 0 deletions src/Turbo/TurboBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
Expand All @@ -23,6 +24,14 @@
*/
final class TurboBundle extends Bundle
{
public const STREAM_FORMAT = 'turbo_stream';
public const STREAM_MEDIA_TYPE = 'text/vnd.turbo-stream.html';

public function boot(): void
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know about this method. Nice to know something like this exists.

{
(new Request())->setFormat(self::STREAM_FORMAT, self::STREAM_MEDIA_TYPE);
}

public function build(ContainerBuilder $container): void
{
parent::build($container);
Expand Down