From 2a92d307926101df2fb55a2f28f2f3482446516d Mon Sep 17 00:00:00 2001 From: Ruslan Lesiutin Date: Fri, 13 Oct 2023 16:59:03 +0100 Subject: [PATCH] feat[react-devtools-extensions/logging]: initialize session id on client for logging --- .../src/registerDevToolsEventLogger.js | 50 +++++++++---------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/packages/react-devtools-shared/src/registerDevToolsEventLogger.js b/packages/react-devtools-shared/src/registerDevToolsEventLogger.js index 45b978b48bf8f..7b47f6c7afecd 100644 --- a/packages/react-devtools-shared/src/registerDevToolsEventLogger.js +++ b/packages/react-devtools-shared/src/registerDevToolsEventLogger.js @@ -12,7 +12,8 @@ import type {LoggerEvent} from 'react-devtools-shared/src/Logger'; import {registerEventLogger} from 'react-devtools-shared/src/Logger'; import {enableLogger} from 'react-devtools-feature-flags'; -let loggingIFrame = null; +let currentLoggingIFrame = null; +let currentSessionId = null; let missedEvents: Array = []; type LoggerContext = { @@ -21,32 +22,27 @@ type LoggerContext = { export function registerDevToolsEventLogger( surface: string, - fetchAdditionalContext: ?() => - | LoggerContext - | ?(() => Promise), + fetchAdditionalContext?: + | (() => LoggerContext) + | (() => Promise), ): void { async function logEvent(event: LoggerEvent) { if (enableLogger) { - if (loggingIFrame != null) { - let metadata = null; - if (event.metadata != null) { - metadata = event.metadata; - // $FlowFixMe[cannot-write]: metadata is not writable and nullable - // $FlowFixMe[prop-missing] - delete event.metadata; - } - loggingIFrame.contentWindow.postMessage( + if (currentLoggingIFrame != null && currentSessionId != null) { + const {metadata, ...eventWithoutMetadata} = event; + const additionalContext: LoggerContext | {} = + fetchAdditionalContext != null ? await fetchAdditionalContext() : {}; + + currentLoggingIFrame?.contentWindow?.postMessage( { source: 'react-devtools-logging', - event: event, + event: eventWithoutMetadata, context: { + ...additionalContext, + metadata: metadata != null ? JSON.stringify(metadata) : '', + session_id: currentSessionId, surface, version: process.env.DEVTOOLS_VERSION, - metadata: metadata !== null ? JSON.stringify(metadata) : '', - ...(fetchAdditionalContext != null - ? // $FlowFixMe[not-an-object] - await fetchAdditionalContext() - : {}), }, }, '*', @@ -58,11 +54,8 @@ export function registerDevToolsEventLogger( } function handleLoggingIFrameLoaded(iframe: HTMLIFrameElement) { - if (loggingIFrame != null) { - return; - } + currentLoggingIFrame = iframe; - loggingIFrame = iframe; if (missedEvents.length > 0) { missedEvents.forEach(event => logEvent(event)); missedEvents = []; @@ -74,18 +67,21 @@ export function registerDevToolsEventLogger( if (enableLogger) { const loggingUrl = process.env.LOGGING_URL; const body = document.body; + if ( typeof loggingUrl === 'string' && loggingUrl.length > 0 && - body != null + body != null && + currentLoggingIFrame == null ) { registerEventLogger(logEvent); + currentSessionId = window.crypto.randomUUID(); const iframe = document.createElement('iframe'); + + iframe.onload = () => handleLoggingIFrameLoaded(iframe); iframe.src = loggingUrl; - iframe.onload = function (...args) { - handleLoggingIFrameLoaded(iframe); - }; + body.appendChild(iframe); } }