[Turbo] add options to EventSource Mercure
| Q | A |
|---|---|
| Bug fix? | yes |
| New feature? | n/a |
| Issues | Fix #291 |
| License | MIT |
To prevent 401 with Mercure & Turbo stream on different domain, add an options for EventSource is required. Exemple of code:
<div id="messages" {{ turbo_stream_listen('https://example.com/books/1', eventSourceOptions = {withCredentials: true}) }}</div>
But I think the mercureAuthorization cookie is not sent. Is it normal ?
It's the Twig Mercure extension that creates the cookie : https://github.com/symfony/mercure/blob/main/src/Twig/MercureExtension.php#L42.
You have right @tibobaldwin for the first call, but after the Turbo takes over. if you are using private updates, Turbo needs to add withCredentials.
But then, how do I add the mercure() Twig function?
Great, this PR is very interesting :). But in fact, how to add this Twig helper which was added in https://github.com/symfony/mercure/pull/62?
Sorry @tibobaldwin & @seb-jean, but I think it's not the place for these questions. Check the issue or the documentation?
But an example:
{% block javascripts %}
<script>
const eventSource = new EventSource("{{ mercure('https://example.com/books/1', { subscribe: ['https://example.com/books/1'] })|escape('js') }}", {
withCredentials: true
});
</script>
{% endblock %}
{% block content %}
<div id="messages" {{ turbo_stream_listen('https://example.com/books/1', eventSourceOptions = {withCredentials: true}) }}
Content
</div>
{% endblock %}
Without this PR, you need to create your own turbo stream controller and call like this:
...
{% block content %}
<div id="messages" data-controller="turbo-stream" data-turbo-stream-topic-value="https://example.com/books/1" data-turbo-stream-hub-value="{{ mercure() }}"
Content
</div>
{% endblock %}
Thanks. This is mainly to set the cookie in fact.
Why this isn't merged yet? Seems totally fine to me, and very helpful.
@ytilotti I think https://github.com/symfony/ux/pull/2447 answers your issue :).
Indeed closing here as the feature is now implemented