Skip to content

[Mercure] Update mercure.rst #15994

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
Oct 25, 2021
Merged
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
10 changes: 6 additions & 4 deletions mercure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ Subscribing to updates in JavaScript from a Twig template is straightforward:
// Will be called every time an update is published by the server
console.log(JSON.parse(event.data));
}
<script>
</script>

The ``mercure()`` Twig function will generate the URL of the Mercure hub
according to the configuration. The URL will include the ``topic`` query
Expand All @@ -260,13 +260,15 @@ parameters corresponding to the topics passed as first argument.
If you want to access to this URL from an external JavaScript file, generate the
URL in a dedicated HTML element:

.. code:: twig
.. code-block:: twig

<script type="application/json" id="mercure-url">{{ mercure('https://example.com/books/1')|json_encode(constant('JSON_UNESCAPED_SLASHES') b-or constant('JSON_HEX_TAG'))|raw }}</script>
<script type="application/json" id="mercure-url">
{{ mercure('https://example.com/books/1')|json_encode(constant('JSON_UNESCAPED_SLASHES') b-or constant('JSON_HEX_TAG'))|raw }}
</script>

Then retrieve it from your JS file:

.. code:: javascript
.. code-block:: javascript

const url = JSON.parse(document.getElementById("mercure-url").textContent);
const eventSource = new EventSource(url);
Expand Down