Skip to content

Commit a68ac90

Browse files
authored
fix(core): Subtract performance.now() from browserPerformanceTimeOrigin fallback (#18715)
This patch fixes a "bug" where we previously returned just `Date.now()` as a fallback for getting the timeOrigin if more precise `performance.timeOrigin` or `performance.timing.navigationStart` were not available or unreliable. This fix now subtracts `performance.now()` from `Date.now()` which should make the fallback more accurate. Closes #18716 (added automatically)
1 parent d0840e9 commit a68ac90

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

packages/core/src/utils/time.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ function getBrowserTimeOrigin(): number | undefined {
125125
return navigationStart;
126126
}
127127

128-
// TODO: We should probably fall back to Date.now() - performance.now(), since this is still more accurate than just Date.now() (?)
129-
// Either both timeOrigin and navigationStart are skewed or neither is available, fallback to Date.
130-
return dateNow;
128+
// Either both timeOrigin and navigationStart are skewed or neither is available, fallback to subtracting
129+
// `performance.now()` from `Date.now()`.
130+
return dateNow - performanceNow;
131131
}
132132

133133
/**

packages/core/test/lib/utils/time.test.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ describe('browserPerformanceTimeOrigin', () => {
2727
vi.unstubAllGlobals();
2828
});
2929

30-
it('returns `Date.now()` if `performance.timeOrigin` is not reliable', async () => {
30+
it('returns `Date.now() - performance.now()` if `performance.timeOrigin` is not reliable', async () => {
3131
const currentTimeMs = 1767778040866;
3232

3333
const unreliableTime = currentTimeMs - RELIABLE_THRESHOLD_MS - 2_000;
3434

35-
const timeSincePageloadMs = 1_234.789;
35+
const timeSincePageloadMs = 1_234.56789;
3636

3737
vi.useFakeTimers();
3838
vi.setSystemTime(new Date(currentTimeMs));
@@ -46,7 +46,29 @@ describe('browserPerformanceTimeOrigin', () => {
4646
});
4747

4848
const timeOrigin = await getFreshPerformanceTimeOrigin();
49-
expect(timeOrigin).toBe(1767778040866);
49+
expect(timeOrigin).toBe(currentTimeMs - timeSincePageloadMs);
50+
51+
vi.useRealTimers();
52+
vi.unstubAllGlobals();
53+
});
54+
55+
it('returns `Date.now() - performance.now()` if neither `performance.timeOrigin` nor `performance.timing.navigationStart` are available', async () => {
56+
const currentTimeMs = 1767778040866;
57+
58+
const timeSincePageloadMs = 1_234.56789;
59+
60+
vi.useFakeTimers();
61+
vi.setSystemTime(new Date(currentTimeMs));
62+
vi.stubGlobal('performance', {
63+
timeOrigin: undefined,
64+
timing: {
65+
navigationStart: undefined,
66+
},
67+
now: () => timeSincePageloadMs,
68+
});
69+
70+
const timeOrigin = await getFreshPerformanceTimeOrigin();
71+
expect(timeOrigin).toBe(currentTimeMs - timeSincePageloadMs);
5072

5173
vi.useRealTimers();
5274
vi.unstubAllGlobals();

0 commit comments

Comments
 (0)