Skip to content

Commit 2a92d30

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

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

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

Lines changed: 23 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,27 @@ type LoggerContext = {
2122

2223
export function registerDevToolsEventLogger(
2324
surface: string,
24-
fetchAdditionalContext: ?() =>
25-
| LoggerContext
26-
| ?(() => Promise<LoggerContext>),
25+
fetchAdditionalContext?:
26+
| (() => LoggerContext)
27+
| (() => Promise<LoggerContext>),
2728
): void {
2829
async function logEvent(event: LoggerEvent) {
2930
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(
31+
if (currentLoggingIFrame != null && currentSessionId != null) {
32+
const {metadata, ...eventWithoutMetadata} = event;
33+
const additionalContext: LoggerContext | {} =
34+
fetchAdditionalContext != null ? await fetchAdditionalContext() : {};
35+
36+
currentLoggingIFrame?.contentWindow?.postMessage(
3937
{
4038
source: 'react-devtools-logging',
41-
event: event,
39+
event: eventWithoutMetadata,
4240
context: {
41+
...additionalContext,
42+
metadata: metadata != null ? JSON.stringify(metadata) : '',
43+
session_id: currentSessionId,
4344
surface,
4445
version: process.env.DEVTOOLS_VERSION,
45-
metadata: metadata !== null ? JSON.stringify(metadata) : '',
46-
...(fetchAdditionalContext != null
47-
? // $FlowFixMe[not-an-object]
48-
await fetchAdditionalContext()
49-
: {}),
5046
},
5147
},
5248
'*',
@@ -58,11 +54,8 @@ export function registerDevToolsEventLogger(
5854
}
5955

6056
function handleLoggingIFrameLoaded(iframe: HTMLIFrameElement) {
61-
if (loggingIFrame != null) {
62-
return;
63-
}
57+
currentLoggingIFrame = iframe;
6458

65-
loggingIFrame = iframe;
6659
if (missedEvents.length > 0) {
6760
missedEvents.forEach(event => logEvent(event));
6861
missedEvents = [];
@@ -74,18 +67,21 @@ export function registerDevToolsEventLogger(
7467
if (enableLogger) {
7568
const loggingUrl = process.env.LOGGING_URL;
7669
const body = document.body;
70+
7771
if (
7872
typeof loggingUrl === 'string' &&
7973
loggingUrl.length > 0 &&
80-
body != null
74+
body != null &&
75+
currentLoggingIFrame == null
8176
) {
8277
registerEventLogger(logEvent);
78+
currentSessionId = window.crypto.randomUUID();
8379

8480
const iframe = document.createElement('iframe');
81+
82+
iframe.onload = () => handleLoggingIFrameLoaded(iframe);
8583
iframe.src = loggingUrl;
86-
iframe.onload = function (...args) {
87-
handleLoggingIFrameLoaded(iframe);
88-
};
84+
8985
body.appendChild(iframe);
9086
}
9187
}

0 commit comments

Comments
 (0)