Skip to content

Commit 9d42463

Browse files
committed
minor #2694 [Turbo] Pass turbo_stream_listen topics to mercure if not defined (Fan2Shrek)
This PR was merged into the 2.x branch. Discussion ---------- [Turbo] Pass `turbo_stream_listen` topics to mercure if not defined | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Docs? | no | Issues | See below | License | MIT Currently, the documentation says: `"If you're using a private hub, you can add { withCredentials: true } as the third argument to turbo_stream_listen() to authenticate with the hub."` However, this doesn’t actually work because the cookie is not created. See: [MercureExtension.php#L75](https://github.com/symfony/mercure/blob/304cf84609ef645d63adc65fc6250292909a461b/src/Twig/MercureExtension.php#L75) To make it work, we need to explicitly specify the topics again. The working call looks like this: `turbo_stream_listen("topics", "default", { subscribe: 'topics', withCredentials: true }` So I updated the implementation, adding the `subscribe` key if it's not already defined. My mistake, sorry 😅 Commits ------- f556d2a Pass topics to mercure if not defined
2 parents 6127799 + f556d2a commit 9d42463

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ public function renderTurboStreamListen(Environment $env, $topic /* array $event
6565
if (isset($eventSourceOptions)) {
6666
try {
6767
$mercure = $this->twig->getExtension(MercureExtension::class);
68-
$mercure->mercure($topics, $eventSourceOptions);
6968

70-
if (isset($eventSourceOptions['withCredentials'])) {
71-
$controllerAttributes['withCredentials'] = $eventSourceOptions['withCredentials'];
69+
if ($eventSourceOptions['withCredentials'] ?? false) {
70+
$eventSourceOptions['subscribe'] ??= $topics;
71+
$controllerAttributes['withCredentials'] = true;
7272
}
73+
74+
$mercure->mercure($topics, $eventSourceOptions);
7375
} catch (RuntimeError $e) {
7476
}
7577
}

0 commit comments

Comments
 (0)