From 9affa421d36e9df700879202063928861cc70a5a Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Mon, 16 Dec 2024 12:53:10 +0100 Subject: [PATCH 1/2] API: save a "flag" on `window` object We need to find a way for people subscribing to the Read the Docs data ready event _after_ it was triggered. This is an idea about how to solve this use case. It adds a "flag" in the `window` object that users can check _before_ subscribing to that event to make usage of the data if the event was triggered already. * Reference: https://github.com/readthedocs/readthedocs.org/issues/10648#issuecomment-2542003070 * Example: https://github.com/readthedocs/test-builds/blob/full-feature/docs/static/readthedocs.js --- src/readthedocs-config.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/readthedocs-config.js b/src/readthedocs-config.js index d597009d..d3330bb4 100644 --- a/src/readthedocs-config.js +++ b/src/readthedocs-config.js @@ -114,6 +114,10 @@ export function getReadTheDocsConfig(sendUrlParam) { // Expose `dataUser` if available or the `data` already requested. const dataEvent = dataUser !== undefined ? dataUser : data; + // Add the data to the window so scripts loaded after the initial + // event was fired can still get access to the data + window.ReadTheDocsEventData = new ReadTheDocsEventData(dataEvent); + // Trigger the addons data ready CustomEvent to with the data the user is expecting. return dispatchEvent( EVENT_READTHEDOCS_ADDONS_DATA_READY, From 8b198efcba8faad5d340869270449a4d210f5f37 Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Mon, 27 Jan 2025 15:42:26 +0100 Subject: [PATCH 2/2] Use `globalThis` --- src/readthedocs-config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/readthedocs-config.js b/src/readthedocs-config.js index d3330bb4..aebdfa94 100644 --- a/src/readthedocs-config.js +++ b/src/readthedocs-config.js @@ -116,7 +116,7 @@ export function getReadTheDocsConfig(sendUrlParam) { // Add the data to the window so scripts loaded after the initial // event was fired can still get access to the data - window.ReadTheDocsEventData = new ReadTheDocsEventData(dataEvent); + globalThis.ReadTheDocsEventData = new ReadTheDocsEventData(dataEvent); // Trigger the addons data ready CustomEvent to with the data the user is expecting. return dispatchEvent(