From 5b8827939d7c80e05eedd76f36111e24f18fda4d Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Thu, 18 Sep 2025 09:57:25 -0700 Subject: [PATCH] Support additional PerformanceResourceTiming fields (#53815) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/53815 Changelog: [Internal] Reviewed By: rubennorte Differential Revision: D82561777 fbshipit-source-id: f387c4bca2112111e736433af90ada16efd14e8b --- .../webperformance/NativePerformance.cpp | 3 ++ .../webperformance/NativePerformance.h | 3 ++ .../react/networking/NetworkReporter.cpp | 14 +++++--- .../react/networking/NetworkReporter.h | 5 ++- .../performance/timeline/PerformanceEntry.h | 5 ++- .../timeline/PerformanceEntryReporter.cpp | 8 ++++- .../timeline/PerformanceEntryReporter.h | 5 ++- .../webapis/performance/ResourceTiming.js | 35 ++++++++++++++++--- .../internals/RawPerformanceEntry.js | 5 ++- .../performance/specs/NativePerformance.js | 3 ++ 10 files changed, 73 insertions(+), 13 deletions(-) diff --git a/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformance.cpp b/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformance.cpp index a0d53eba212..c1c8d70167c 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformance.cpp +++ b/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformance.cpp @@ -75,6 +75,9 @@ NativePerformanceEntry toNativePerformanceEntry(const PerformanceEntry& entry) { nativeEntry.responseStart = resourceEntry.responseStart; nativeEntry.responseEnd = resourceEntry.responseEnd; nativeEntry.responseStatus = resourceEntry.responseStatus; + nativeEntry.contentType = resourceEntry.contentType; + nativeEntry.encodedBodySize = resourceEntry.encodedBodySize; + nativeEntry.decodedBodySize = resourceEntry.decodedBodySize; } return nativeEntry; diff --git a/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformance.h b/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformance.h index 21033caf956..d065889205a 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformance.h +++ b/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformance.h @@ -68,6 +68,9 @@ struct NativePerformanceEntry { std::optional responseStart; std::optional responseEnd; std::optional responseStatus; + std::optional contentType; + std::optional encodedBodySize; + std::optional decodedBodySize; }; template <> diff --git a/packages/react-native/ReactCommon/react/networking/NetworkReporter.cpp b/packages/react-native/ReactCommon/react/networking/NetworkReporter.cpp index fbc34603753..30d32d37f98 100644 --- a/packages/react-native/ReactCommon/react/networking/NetworkReporter.cpp +++ b/packages/react-native/ReactCommon/react/networking/NetworkReporter.cpp @@ -9,10 +9,10 @@ #ifdef REACT_NATIVE_DEBUGGER_ENABLED #include -#include #include #include #endif +#include #include #include @@ -110,6 +110,7 @@ void NetworkReporter::reportResponseStart( const ResponseInfo& responseInfo, int encodedDataLength) { auto now = HighResTimeStamp::now(); + auto headers = responseInfo.headers.value_or(Headers{}); if (ReactNativeFeatureFlags::enableResourceTimingAPI()) { // All builds: Annotate PerformanceResourceTiming metadata @@ -117,16 +118,18 @@ void NetworkReporter::reportResponseStart( std::lock_guard lock(perfTimingsMutex_); auto it = perfTimingsBuffer_.find(requestId); if (it != perfTimingsBuffer_.end()) { + auto contentType = jsinspector_modern::mimeTypeFromHeaders(headers); it->second.connectEnd = now; it->second.responseStart = now; it->second.responseStatus = responseInfo.statusCode; + it->second.contentType = contentType; + it->second.encodedBodySize = encodedDataLength; + it->second.decodedBodySize = encodedDataLength; } } } #ifdef REACT_NATIVE_DEBUGGER_ENABLED - auto headers = responseInfo.headers.value_or(Headers{}); - // Debugger enabled: CDP event handling jsinspector_modern::NetworkHandler::getInstance().onResponseReceived( requestId, @@ -174,7 +177,10 @@ void NetworkReporter::reportResponseEnd( eventData.connectEnd.value_or(now), eventData.responseStart.value_or(now), now, - eventData.responseStatus); + eventData.responseStatus, + eventData.contentType, + eventData.encodedBodySize, + eventData.decodedBodySize); perfTimingsBuffer_.erase(requestId); } } diff --git a/packages/react-native/ReactCommon/react/networking/NetworkReporter.h b/packages/react-native/ReactCommon/react/networking/NetworkReporter.h index 1aaa11082a1..b68222fa37d 100644 --- a/packages/react-native/ReactCommon/react/networking/NetworkReporter.h +++ b/packages/react-native/ReactCommon/react/networking/NetworkReporter.h @@ -33,7 +33,10 @@ struct ResourceTimingData { std::optional connectStart; std::optional connectEnd; std::optional responseStart; - std::optional responseStatus; + int responseStatus = 0; + std::string contentType; + int encodedBodySize = 0; + int decodedBodySize = 0; }; /** diff --git a/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntry.h b/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntry.h index b3086e9c24b..dbccf4ba844 100644 --- a/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntry.h +++ b/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntry.h @@ -69,7 +69,10 @@ struct PerformanceResourceTiming : AbstractPerformanceEntry { std::optional responseStart; /** Aligns with `duration`. */ std::optional responseEnd; - std::optional responseStatus; + int responseStatus; + std::string contentType; + int encodedBodySize; + int decodedBodySize; }; using PerformanceEntry = std::variant< diff --git a/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntryReporter.cpp b/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntryReporter.cpp index d4c6423b231..05e75a7d488 100644 --- a/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntryReporter.cpp +++ b/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntryReporter.cpp @@ -307,7 +307,10 @@ void PerformanceEntryReporter::reportResourceTiming( std::optional connectEnd, HighResTimeStamp responseStart, HighResTimeStamp responseEnd, - const std::optional& responseStatus) { + int responseStatus, + const std::string& contentType, + int encodedBodySize, + int decodedBodySize) { const auto entry = PerformanceResourceTiming{ {.name = url, .startTime = fetchStart}, fetchStart, @@ -317,6 +320,9 @@ void PerformanceEntryReporter::reportResourceTiming( responseStart, responseEnd, responseStatus, + contentType, + encodedBodySize, + decodedBodySize, }; // Add to buffers & notify observers diff --git a/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntryReporter.h b/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntryReporter.h index 938f3fcb2f8..3e2c3e67456 100644 --- a/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntryReporter.h +++ b/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntryReporter.h @@ -118,7 +118,10 @@ class PerformanceEntryReporter { std::optional connectEnd, HighResTimeStamp responseStart, HighResTimeStamp responseEnd, - const std::optional& responseStatus); + int responseStatus, + const std::string& contentType, + int encodedBodySize, + int decodedBodySize); private: std::unique_ptr observerRegistry_; diff --git a/packages/react-native/src/private/webapis/performance/ResourceTiming.js b/packages/react-native/src/private/webapis/performance/ResourceTiming.js index 8e1895d0934..4154ca155b4 100644 --- a/packages/react-native/src/private/webapis/performance/ResourceTiming.js +++ b/packages/react-native/src/private/webapis/performance/ResourceTiming.js @@ -25,7 +25,10 @@ export type PerformanceResourceTimingJSON = { connectEnd: DOMHighResTimeStamp, responseStart: DOMHighResTimeStamp, responseEnd: DOMHighResTimeStamp, - responseStatus: ?number, + responseStatus: number, + contentType: string, + encodedBodySize: number, + decodedBodySize: number, ... }; @@ -39,7 +42,10 @@ export interface PerformanceResourceTimingInit { +connectEnd: DOMHighResTimeStamp; +responseStart: DOMHighResTimeStamp; +responseEnd: DOMHighResTimeStamp; - +responseStatus?: number; + +responseStatus: number; + +contentType: string; + +encodedBodySize: number; + +decodedBodySize: number; } export class PerformanceResourceTiming extends PerformanceEntry { @@ -49,7 +55,10 @@ export class PerformanceResourceTiming extends PerformanceEntry { #connectEnd: DOMHighResTimeStamp; #responseStart: DOMHighResTimeStamp; #responseEnd: DOMHighResTimeStamp; - #responseStatus: ?number; + #responseStatus: number; + #contentType: string; + #encodedBodySize: number; + #decodedBodySize: number; constructor(init: PerformanceResourceTimingInit) { super('resource', init); @@ -61,6 +70,9 @@ export class PerformanceResourceTiming extends PerformanceEntry { this.#responseStart = init.responseStart; this.#responseEnd = init.responseEnd; this.#responseStatus = init.responseStatus; + this.#contentType = init.contentType; + this.#encodedBodySize = init.encodedBodySize; + this.#decodedBodySize = init.decodedBodySize; } get fetchStart(): DOMHighResTimeStamp { @@ -87,10 +99,22 @@ export class PerformanceResourceTiming extends PerformanceEntry { return this.#responseEnd; } - get responseStatus(): ?number { + get responseStatus(): number { return this.#responseStatus; } + get contentType(): string { + return this.#contentType; + } + + get encodedBodySize(): number { + return this.#encodedBodySize; + } + + get decodedBodySize(): number { + return this.#decodedBodySize; + } + toJSON(): PerformanceResourceTimingJSON { return { ...super.toJSON(), @@ -101,6 +125,9 @@ export class PerformanceResourceTiming extends PerformanceEntry { responseStart: this.#responseStart, responseEnd: this.#responseEnd, responseStatus: this.#responseStatus, + contentType: this.contentType, + encodedBodySize: this.encodedBodySize, + decodedBodySize: this.decodedBodySize, }; } } diff --git a/packages/react-native/src/private/webapis/performance/internals/RawPerformanceEntry.js b/packages/react-native/src/private/webapis/performance/internals/RawPerformanceEntry.js index 40854b834ba..6ef10b6980b 100644 --- a/packages/react-native/src/private/webapis/performance/internals/RawPerformanceEntry.js +++ b/packages/react-native/src/private/webapis/performance/internals/RawPerformanceEntry.js @@ -68,7 +68,10 @@ export function rawToPerformanceEntry( connectEnd: entry.connectEnd ?? 0, responseStart: entry.responseStart ?? 0, responseEnd: entry.responseEnd ?? 0, - responseStatus: entry.responseStatus, + responseStatus: entry.responseStatus ?? 0, + contentType: entry.contentType ?? '', + encodedBodySize: entry.encodedBodySize ?? 0, + decodedBodySize: entry.decodedBodySize ?? 0, }); default: return new PerformanceEntry(rawToPerformanceEntryType(entry.entryType), { diff --git a/packages/react-native/src/private/webapis/performance/specs/NativePerformance.js b/packages/react-native/src/private/webapis/performance/specs/NativePerformance.js index e191d0a6ac4..af194df4351 100644 --- a/packages/react-native/src/private/webapis/performance/specs/NativePerformance.js +++ b/packages/react-native/src/private/webapis/performance/specs/NativePerformance.js @@ -37,6 +37,9 @@ export type RawPerformanceEntry = { responseStart?: number, responseEnd?: number, responseStatus?: number, + contentType?: string, + encodedBodySize?: number, + decodedBodySize?: number, }; export opaque type OpaqueNativeObserverHandle = mixed;