From 6d85ac65dcf5b88185b36f3fa2e69b4d5532f1d3 Mon Sep 17 00:00:00 2001 From: Argho Roy Date: Thu, 10 Oct 2024 14:13:10 -0400 Subject: [PATCH 1/9] Remove backend trace id for cached responses --- lib/resources/timingSerializer.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/resources/timingSerializer.ts b/lib/resources/timingSerializer.ts index 9d5b13a9b..adee08db5 100644 --- a/lib/resources/timingSerializer.ts +++ b/lib/resources/timingSerializer.ts @@ -10,12 +10,14 @@ export function serializeEntryToArray(entry: PerformanceResourceTiming) { // When timing data is available, we can provide additional information about // caching and resource sizes. + let isCached = false; if (typeof entry['transferSize'] === 'number' && typeof entry['encodedBodySize'] === 'number' && // All this information may not be available due to the timing allow origin check. entry['encodedBodySize'] > 0) { if (entry['transferSize'] === 0) { result.push(cachingTypes.cached); + isCached = true; } else if (entry['transferSize'] > 0 && (entry['encodedBodySize'] === 0 || entry['transferSize'] < entry['encodedBodySize'])) { result.push(cachingTypes.validated); } else { @@ -68,19 +70,21 @@ export function serializeEntryToArray(entry: PerformanceResourceTiming) { } let backendTraceId = ''; - try { - const serverTimings = entry['serverTiming']; - if (serverTimings instanceof Array) { - for (let i = 0; i < serverTimings.length; i++) { - const serverTiming = serverTimings[i]; - if (serverTiming['name'] === vars.serverTimingBackendTraceIdEntryName) { - backendTraceId = serverTiming['description']; + if (!isCached){ + try { + const serverTimings = entry['serverTiming']; + if (serverTimings instanceof Array) { + for (let i = 0; i < serverTimings.length; i++) { + const serverTiming = serverTimings[i]; + if (serverTiming['name'] === vars.serverTimingBackendTraceIdEntryName) { + backendTraceId = serverTiming['description']; + } } } + } catch (e) { + // Some browsers may not grant access to the field when the Timing-Allow-Origin + // check fails. Better be safe than sorry here. } - } catch (e) { - // Some browsers may not grant access to the field when the Timing-Allow-Origin - // check fails. Better be safe than sorry here. } result.push(backendTraceId); From 3c335c8d03045f95fa841891b7d60251dc13050a Mon Sep 17 00:00:00 2001 From: Argho Roy Date: Wed, 16 Oct 2024 11:16:58 -0400 Subject: [PATCH 2/9] Removed backendTraceId from responses with cache interaction - browser cache with validation --- lib/resources/timingSerializer.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/resources/timingSerializer.ts b/lib/resources/timingSerializer.ts index adee08db5..5599c5f63 100644 --- a/lib/resources/timingSerializer.ts +++ b/lib/resources/timingSerializer.ts @@ -20,6 +20,7 @@ export function serializeEntryToArray(entry: PerformanceResourceTiming) { isCached = true; } else if (entry['transferSize'] > 0 && (entry['encodedBodySize'] === 0 || entry['transferSize'] < entry['encodedBodySize'])) { result.push(cachingTypes.validated); + isCached = true; } else { result.push(cachingTypes.fullLoad); } From d014cae86786d2c4b041e8b36d22fefd56040d46 Mon Sep 17 00:00:00 2001 From: Argho Roy Date: Wed, 23 Oct 2024 12:30:39 -0400 Subject: [PATCH 3/9] Added unit tests for caching fix --- test/unit/resources.test.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/test/unit/resources.test.js b/test/unit/resources.test.js index bb398e932..3a9b390ba 100644 --- a/test/unit/resources.test.js +++ b/test/unit/resources.test.js @@ -57,7 +57,9 @@ describe('resources/timingSerializer', () => { decodedBodySize: 16786, serverTiming: [] }; - expect(toHumanReadableEntry(serializeEntry(entry))).toMatchSnapshot(); + const result = toHumanReadableEntry(serializeEntry(entry)); + expect(result).toMatchSnapshot(); + expect(result['backendTraceId']).toBe(""); }); it('must identify full asset retrieval', () => { @@ -90,7 +92,9 @@ describe('resources/timingSerializer', () => { } ] }; - expect(toHumanReadableEntry(serializeEntry(entry))).toMatchSnapshot(); + const result = toHumanReadableEntry(serializeEntry(entry)); + expect(result).toMatchSnapshot(); + expect(result['backendTraceId']).not.toBeNull(); }); it('must identify cache validation', () => { @@ -118,7 +122,9 @@ describe('resources/timingSerializer', () => { decodedBodySize: 16786, serverTiming: [] }; - expect(toHumanReadableEntry(serializeEntry(entry))).toMatchSnapshot(); + const result = toHumanReadableEntry(serializeEntry(entry)); + expect(result).toMatchSnapshot(); + expect(result['backendTraceId']).toBe(""); }); }); }); From 9029410ba5a1810634c856e881e50bbdc5c74acb Mon Sep 17 00:00:00 2001 From: Argho Roy Date: Wed, 23 Oct 2024 12:32:14 -0400 Subject: [PATCH 4/9] Added change to CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51bd86692..749930764 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Upgrade dev packages to address security issues - Refactored code for autoPageDetection - Fix autoPageDetection to set page name for initial page load +- Remove backendTraceId for cached responses ## 1.7.2 From a5d44e7293c6de563093a11193a8cd1de4b61e45 Mon Sep 17 00:00:00 2001 From: Argho Roy Date: Wed, 23 Oct 2024 12:43:19 -0400 Subject: [PATCH 5/9] Added info log --- lib/resources/timingSerializer.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/resources/timingSerializer.ts b/lib/resources/timingSerializer.ts index 5599c5f63..549ebbf2b 100644 --- a/lib/resources/timingSerializer.ts +++ b/lib/resources/timingSerializer.ts @@ -1,4 +1,5 @@ import { cachingTypes, initiatorTypes } from './consts'; +import {info} from '../debug'; import vars from '../vars'; export function serializeEntryToArray(entry: PerformanceResourceTiming) { @@ -87,6 +88,9 @@ export function serializeEntryToArray(entry: PerformanceResourceTiming) { // check fails. Better be safe than sorry here. } } + else { + info("Response is cached, removed backendTraceId from response") + } result.push(backendTraceId); if (hasValidTimings) { From 849ccd4c2e0f03adcf196adb6033b9491a286325 Mon Sep 17 00:00:00 2001 From: Argho Roy Date: Wed, 23 Oct 2024 12:48:11 -0400 Subject: [PATCH 6/9] Changed strings to singlequotes --- test/unit/resources.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/resources.test.js b/test/unit/resources.test.js index 3a9b390ba..34af4eeba 100644 --- a/test/unit/resources.test.js +++ b/test/unit/resources.test.js @@ -59,7 +59,7 @@ describe('resources/timingSerializer', () => { }; const result = toHumanReadableEntry(serializeEntry(entry)); expect(result).toMatchSnapshot(); - expect(result['backendTraceId']).toBe(""); + expect(result['backendTraceId']).toBe(''); }); it('must identify full asset retrieval', () => { @@ -124,7 +124,7 @@ describe('resources/timingSerializer', () => { }; const result = toHumanReadableEntry(serializeEntry(entry)); expect(result).toMatchSnapshot(); - expect(result['backendTraceId']).toBe(""); + expect(result['backendTraceId']).toBe(''); }); }); }); From 576b5645023efa1b187ef3ee92c19d1c96ba07c3 Mon Sep 17 00:00:00 2001 From: Argho Roy Date: Wed, 23 Oct 2024 13:27:02 -0400 Subject: [PATCH 7/9] Fix build errors --- lib/resources/timingSerializer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/resources/timingSerializer.ts b/lib/resources/timingSerializer.ts index 549ebbf2b..8b2de0b7e 100644 --- a/lib/resources/timingSerializer.ts +++ b/lib/resources/timingSerializer.ts @@ -89,7 +89,7 @@ export function serializeEntryToArray(entry: PerformanceResourceTiming) { } } else { - info("Response is cached, removed backendTraceId from response") + info('Response is cached, removed backendTraceId from response'); } result.push(backendTraceId); From 16d656c6e8bc3a7c878f600ded09e165c04c65c0 Mon Sep 17 00:00:00 2001 From: Helen Jiang Date: Thu, 24 Oct 2024 09:09:16 -0700 Subject: [PATCH 8/9] hjtest --- lib/resources/timingSerializer.ts | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/resources/timingSerializer.ts b/lib/resources/timingSerializer.ts index 8b2de0b7e..773a7e232 100644 --- a/lib/resources/timingSerializer.ts +++ b/lib/resources/timingSerializer.ts @@ -72,25 +72,27 @@ export function serializeEntryToArray(entry: PerformanceResourceTiming) { } let backendTraceId = ''; - if (!isCached){ - try { - const serverTimings = entry['serverTiming']; - if (serverTimings instanceof Array) { - for (let i = 0; i < serverTimings.length; i++) { - const serverTiming = serverTimings[i]; - if (serverTiming['name'] === vars.serverTimingBackendTraceIdEntryName) { - backendTraceId = serverTiming['description']; + try { + const serverTimings = entry['serverTiming']; + if (serverTimings instanceof Array) { + for (const element of serverTimings) { + const serverTiming = element; + if (serverTiming['name'] === vars.serverTimingBackendTraceIdEntryName) { + backendTraceId = serverTiming['description']; + if (isCached) { + if (DEBUG) { + info('Response is cached, removed backendTraceId from response'); + } + backendTraceId = ''; } } } - } catch (e) { - // Some browsers may not grant access to the field when the Timing-Allow-Origin - // check fails. Better be safe than sorry here. } + } catch (e) { + // Some browsers may not grant access to the field when the Timing-Allow-Origin + // check fails. Better be safe than sorry here. } - else { - info('Response is cached, removed backendTraceId from response'); - } + result.push(backendTraceId); if (hasValidTimings) { From 67ef08d457cad4a84bc596393ddc07e2f590c264 Mon Sep 17 00:00:00 2001 From: Helen Jiang Date: Thu, 24 Oct 2024 09:11:50 -0700 Subject: [PATCH 9/9] hjtest 2 --- lib/resources/timingSerializer.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/resources/timingSerializer.ts b/lib/resources/timingSerializer.ts index 773a7e232..c7f7219b5 100644 --- a/lib/resources/timingSerializer.ts +++ b/lib/resources/timingSerializer.ts @@ -75,8 +75,8 @@ export function serializeEntryToArray(entry: PerformanceResourceTiming) { try { const serverTimings = entry['serverTiming']; if (serverTimings instanceof Array) { - for (const element of serverTimings) { - const serverTiming = element; + for (let i = 0; i < serverTimings.length; i++) { + const serverTiming = serverTimings[i]; if (serverTiming['name'] === vars.serverTimingBackendTraceIdEntryName) { backendTraceId = serverTiming['description']; if (isCached) {