Skip to content

Commit a0f9a1b

Browse files
committed
apply more changes, fix size limit
1 parent 1b4dc50 commit a0f9a1b

File tree

14 files changed

+52
-100
lines changed

14 files changed

+52
-100
lines changed

.size-limit.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ module.exports = [
120120
import: createImport('init', 'ErrorBoundary', 'reactRouterV6BrowserTracingIntegration'),
121121
ignore: ['react/jsx-runtime'],
122122
gzip: true,
123-
limit: '40.5 KB',
123+
limit: '41 KB',
124124
},
125125
// Vue SDK (ESM)
126126
{
@@ -215,7 +215,7 @@ module.exports = [
215215
import: createImport('init'),
216216
ignore: ['$app/stores'],
217217
gzip: true,
218-
limit: '39 KB',
218+
limit: '40 KB',
219219
},
220220
// Node SDK (ESM)
221221
{

packages/browser-utils/src/metrics/cls.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export function trackClsAsStandaloneSpan(): void {
5858
standaloneClsEntry = entry;
5959
}, true);
6060

61+
// TODO: Figure out if we can switch to using whenIdleOrHidden instead of onHidden
6162
// use pagehide event from web-vitals
6263
onHidden(() => {
6364
_collectClsOnce();

packages/browser-utils/src/metrics/web-vitals/getFID.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export const onFID = (onReport: (metric: FIDMetric) => void, opts: ReportOpts =
6464
report = bindReporter(onReport, metric, FIDThresholds, opts.reportAllChanges);
6565

6666
if (po) {
67+
// sentry: TODO: Figure out if we can use new whinIdleOrHidden insteard of onHidden
6768
onHidden(
6869
runOnce(() => {
6970
handleEntries(po.takeRecords() as FIDMetric['entries']);

packages/browser-utils/src/metrics/web-vitals/lib/bindReporter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const bindReporter = <MetricName extends MetricType['name']>(
3737
return (forceReport?: boolean) => {
3838
if (metric.value >= 0) {
3939
if (forceReport || reportAllChanges) {
40-
delta = metric.value - (prevValue || 0);
40+
delta = metric.value - (prevValue ?? 0);
4141

4242
// Report the metric if there's a non-zero delta or if no previous
4343
// value exists (which can happen in the case of the document becoming

packages/browser-utils/src/metrics/web-vitals/lib/generateUniqueID.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
* @return {string}
2121
*/
2222
export const generateUniqueID = () => {
23-
return `v4-${Date.now()}-${Math.floor(Math.random() * (9e12 - 1)) + 1e12}`;
23+
return `v5-${Date.now()}-${Math.floor(Math.random() * (9e12 - 1)) + 1e12}`;
2424
};

packages/browser-utils/src/metrics/web-vitals/lib/getActivationStart.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ import { getNavigationEntry } from './getNavigationEntry';
1818

1919
export const getActivationStart = (): number => {
2020
const navEntry = getNavigationEntry();
21-
return navEntry?.activationStart || 0;
21+
return navEntry?.activationStart ?? 0;
2222
};

packages/browser-utils/src/metrics/web-vitals/lib/getNavigationEntry.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import { WINDOW } from '../../../types';
2121
export const getNavigationEntry = (checkResponseStart = true): PerformanceNavigationTiming | void => {
2222
const navigationEntry = WINDOW.performance?.getEntriesByType?.('navigation')[0];
2323
// Check to ensure the `responseStart` property is present and valid.
24-
// In some cases no value is reported by the browser (for
24+
// In some cases a zero value is reported by the browser (for
2525
// privacy/security reasons), and in other cases (bugs) the value is
2626
// negative or is larger than the current page time. Ignore these cases:
27-
// https://github.com/GoogleChrome/web-vitals/issues/137
28-
// https://github.com/GoogleChrome/web-vitals/issues/162
29-
// https://github.com/GoogleChrome/web-vitals/issues/275
27+
// - https://github.com/GoogleChrome/web-vitals/issues/137
28+
// - https://github.com/GoogleChrome/web-vitals/issues/162
29+
// - https://github.com/GoogleChrome/web-vitals/issues/275
3030
if (
3131
// sentry-specific change:
3232
// We don't want to check for responseStart for our own use of `getNavigationEntry`

packages/browser-utils/src/metrics/web-vitals/lib/getVisibilityWatcher.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { WINDOW } from '../../../types';
18+
import { getActivationStart } from './getActivationStart';
1819

1920
let firstHiddenTime = -1;
2021

@@ -24,7 +25,7 @@ const initHiddenTime = () => {
2425
// that visibility state is always 'hidden' during prerendering, so we have
2526
// to ignore that case until prerendering finishes (see: `prerenderingchange`
2627
// event logic below).
27-
return WINDOW.document!.visibilityState === 'hidden' && !WINDOW.document!.prerendering ? 0 : Infinity;
28+
return WINDOW.document?.visibilityState === 'hidden' && !WINDOW.document?.prerendering ? 0 : Infinity;
2829
};
2930

3031
const onVisibilityUpdate = (event: Event) => {
@@ -61,11 +62,22 @@ const removeChangeListeners = () => {
6162

6263
export const getVisibilityWatcher = () => {
6364
if (WINDOW.document && firstHiddenTime < 0) {
64-
// If the document is hidden when this code runs, assume it was hidden
65-
// since navigation start. This isn't a perfect heuristic, but it's the
66-
// best we can do until an API is available to support querying past
67-
// visibilityState.
68-
firstHiddenTime = initHiddenTime();
65+
// Check if we have a previous hidden `visibility-state` performance entry.
66+
const activationStart = getActivationStart();
67+
const firstVisibilityStateHiddenTime = !WINDOW.document.prerendering
68+
? globalThis.performance
69+
.getEntriesByType('visibility-state')
70+
.filter(e => e.name === 'hidden' && e.startTime > activationStart)[0]?.startTime
71+
: undefined;
72+
73+
// Prefer that, but if it's not available and the document is hidden when
74+
// this code runs, assume it was hidden since navigation start. This isn't
75+
// a perfect heuristic, but it's the best we can do until the
76+
// `visibility-state` performance entry becomes available in all browsers.
77+
firstHiddenTime = firstVisibilityStateHiddenTime ?? initHiddenTime();
78+
// We're still going to listen to for changes so we can handle things like
79+
// bfcache restores and/or prerender without having to examine individual
80+
// timestamps in detail.
6981
addChangeListeners();
7082
}
7183
return {

packages/browser-utils/src/metrics/web-vitals/lib/initMetric.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { generateUniqueID } from './generateUniqueID';
2020
import { getActivationStart } from './getActivationStart';
2121
import { getNavigationEntry } from './getNavigationEntry';
2222

23-
export const initMetric = <MetricName extends MetricType['name']>(name: MetricName, value?: number) => {
23+
export const initMetric = <MetricName extends MetricType['name']>(name: MetricName, value: number = -1) => {
2424
const navEntry = getNavigationEntry();
2525
let navigationType: MetricType['navigationType'] = 'navigate';
2626

@@ -39,7 +39,7 @@ export const initMetric = <MetricName extends MetricType['name']>(name: MetricNa
3939

4040
return {
4141
name,
42-
value: typeof value === 'undefined' ? -1 : value,
42+
value,
4343
rating: 'good' as const, // If needed, will be updated when reported. `const` to keep the type from widening to `string`.
4444
delta: 0,
4545
entries,

packages/browser-utils/src/metrics/web-vitals/lib/observe.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ interface PerformanceEntryMap {
4141
export const observe = <K extends keyof PerformanceEntryMap>(
4242
type: K,
4343
callback: (entries: PerformanceEntryMap[K]) => void,
44-
opts?: PerformanceObserverInit,
44+
opts: PerformanceObserverInit = {},
4545
): PerformanceObserver | undefined => {
4646
try {
4747
if (PerformanceObserver.supportedEntryTypes.includes(type)) {
@@ -54,18 +54,10 @@ export const observe = <K extends keyof PerformanceEntryMap>(
5454
callback(list.getEntries() as PerformanceEntryMap[K]);
5555
});
5656
});
57-
po.observe(
58-
Object.assign(
59-
{
60-
type,
61-
buffered: true,
62-
},
63-
opts || {},
64-
) as PerformanceObserverInit,
65-
);
57+
po.observe({ type, buffered: true, ...opts });
6658
return po;
6759
}
68-
} catch (e) {
60+
} catch {
6961
// Do nothing.
7062
}
7163
return;

0 commit comments

Comments
 (0)