Skip to content

Commit 2c1b05e

Browse files
committed
feat[react-devtools-extensions/logging]: initialize session id on client for logging
1 parent 1b4ba74 commit 2c1b05e

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

packages/react-devtools-shared/src/registerDevToolsEventLogger.js

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import type {LoggerEvent} from 'react-devtools-shared/src/Logger';
1212
import {registerEventLogger} from 'react-devtools-shared/src/Logger';
1313
import {enableLogger} from 'react-devtools-feature-flags';
1414

15-
let loggingIFrame = null;
15+
let currentLoggingIFrame = null;
16+
let currentSessionId = null;
1617
let missedEvents: Array<LoggerEvent> = [];
1718

1819
type LoggerContext = {
@@ -21,32 +22,24 @@ type LoggerContext = {
2122

2223
export function registerDevToolsEventLogger(
2324
surface: string,
24-
fetchAdditionalContext: ?() =>
25-
| LoggerContext
26-
| ?(() => Promise<LoggerContext>),
25+
fetchAdditionalContext?: (() => LoggerContext) | (() => Promise<LoggerContext>),
2726
): void {
2827
async function logEvent(event: LoggerEvent) {
2928
if (enableLogger) {
30-
if (loggingIFrame != null) {
31-
let metadata = null;
32-
if (event.metadata != null) {
33-
metadata = event.metadata;
34-
// $FlowFixMe[cannot-write]: metadata is not writable and nullable
35-
// $FlowFixMe[prop-missing]
36-
delete event.metadata;
37-
}
38-
loggingIFrame.contentWindow.postMessage(
29+
if (currentLoggingIFrame != null && currentSessionId != null) {
30+
const {metadata, ...eventWithoutMetadata} = event;
31+
const additionalContext: LoggerContext | {} = fetchAdditionalContext != null ? await fetchAdditionalContext() : {};
32+
33+
currentLoggingIFrame?.contentWindow?.postMessage(
3934
{
4035
source: 'react-devtools-logging',
41-
event: event,
36+
event: eventWithoutMetadata,
4237
context: {
38+
...additionalContext,
39+
metadata: metadata != null ? JSON.stringify(metadata) : '',
40+
session_id: currentSessionId,
4341
surface,
4442
version: process.env.DEVTOOLS_VERSION,
45-
metadata: metadata !== null ? JSON.stringify(metadata) : '',
46-
...(fetchAdditionalContext != null
47-
? // $FlowFixMe[not-an-object]
48-
await fetchAdditionalContext()
49-
: {}),
5043
},
5144
},
5245
'*',
@@ -58,11 +51,8 @@ export function registerDevToolsEventLogger(
5851
}
5952

6053
function handleLoggingIFrameLoaded(iframe: HTMLIFrameElement) {
61-
if (loggingIFrame != null) {
62-
return;
63-
}
54+
currentLoggingIFrame = iframe;
6455

65-
loggingIFrame = iframe;
6656
if (missedEvents.length > 0) {
6757
missedEvents.forEach(event => logEvent(event));
6858
missedEvents = [];
@@ -74,18 +64,21 @@ export function registerDevToolsEventLogger(
7464
if (enableLogger) {
7565
const loggingUrl = process.env.LOGGING_URL;
7666
const body = document.body;
67+
7768
if (
7869
typeof loggingUrl === 'string' &&
7970
loggingUrl.length > 0 &&
80-
body != null
71+
body != null &&
72+
currentLoggingIFrame == null
8173
) {
8274
registerEventLogger(logEvent);
75+
currentSessionId = window.crypto.randomUUID();
8376

8477
const iframe = document.createElement('iframe');
78+
79+
iframe.onload = () => handleLoggingIFrameLoaded(iframe);
8580
iframe.src = loggingUrl;
86-
iframe.onload = function (...args) {
87-
handleLoggingIFrameLoaded(iframe);
88-
};
81+
8982
body.appendChild(iframe);
9083
}
9184
}

0 commit comments

Comments
 (0)