Skip to content

Skipping your own refresh broadcast

A Turbo 8 page refresh broadcast (<turbo-stream action="refresh">, over Mercure for example) reaches every tab showing the page, the one that made the change included. That tab already renders the change from its own response: the refresh reloads it a second time.

Turbo sends an X-Turbo-Request-Id header with each of its fetch requests and remembers the last ones. When a refresh stream carries one of them as its request-id, the tab that sent the request skips it. TurboManager reads that header, so the broadcast can carry it:

use Silarhi\TurboBundle\TurboManager;
use Symfony\Component\Mercure\HubInterface;
use Symfony\Component\Mercure\Update;
use Symfony\UX\Turbo\Helper\TurboStream;
public function edit(Request $request, Project $item, HubInterface $hub, TurboManager $turbo): Response
{
// …
$hub->publish(new Update('projects', TurboStream::refresh($turbo->getTurboRequestId())));
return $this->redirectToRoute('projects_show', ['id' => $item->getId()]);
}

In a template rendered for the broadcast, use the turbo_request_id() Twig function:

<turbo-stream action="refresh" request-id="{{ turbo_request_id() }}"></turbo-stream>

getTurboRequestId() and turbo_request_id() return null when the header is missing: on Turbo 7, which sends none, on a request Turbo did not send, and without a current request. A refresh stream without a request-id (or with an empty one) refreshes every tab.

Not to be confused with turbo_frame_request_id() of symfony/ux-turbo, which returns the id of the requesting frame (the Turbo-Frame header), like turbo_frame_id().