Skip to content

Commit

Permalink
Add even more baselinei updates
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed Sep 27, 2024
1 parent d148da5 commit ee6cfb2
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 83 deletions.
74 changes: 37 additions & 37 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const configurePlugins = ({module}) => {
[
'@babel/preset-env',
{
bugfixes: true,
targets: [
// Match browsers supporting "Baseline Widely Available" features:
// https://web.dev/baseline
Expand All @@ -35,7 +36,6 @@ const configurePlugins = ({module}) => {
'and_ff >0 and last 2.5 years',
'ios >0 and last 2.5 years',
],
bugfixes: true,
},
],
],
Expand All @@ -57,24 +57,24 @@ const configs = [
},
plugins: configurePlugins({module: true}),
},
// {
// input: 'dist/modules/index.js',
// output: {
// format: 'umd',
// file: `./dist/web-vitals.umd.cjs`,
// name: 'webVitals',
// },
// plugins: configurePlugins({module: false}),
// },
// {
// input: 'dist/modules/index.js',
// output: {
// format: 'iife',
// file: './dist/web-vitals.iife.js',
// name: 'webVitals',
// },
// plugins: configurePlugins({module: false}),
// },
{
input: 'dist/modules/index.js',
output: {
format: 'umd',
file: `./dist/web-vitals.umd.cjs`,
name: 'webVitals',
},
plugins: configurePlugins({module: false}),
},
{
input: 'dist/modules/index.js',
output: {
format: 'iife',
file: './dist/web-vitals.iife.js',
name: 'webVitals',
},
plugins: configurePlugins({module: false}),
},
{
input: 'dist/modules/attribution/index.js',
output: {
Expand All @@ -83,24 +83,24 @@ const configs = [
},
plugins: configurePlugins({module: true}),
},
// {
// input: 'dist/modules/attribution/index.js',
// output: {
// format: 'umd',
// file: `./dist/web-vitals.attribution.umd.cjs`,
// name: 'webVitals',
// },
// plugins: configurePlugins({module: false}),
// },
// {
// input: 'dist/modules/attribution/index.js',
// output: {
// format: 'iife',
// file: './dist/web-vitals.attribution.iife.js',
// name: 'webVitals',
// },
// plugins: configurePlugins({module: false}),
// },
{
input: 'dist/modules/attribution/index.js',
output: {
format: 'umd',
file: `./dist/web-vitals.attribution.umd.cjs`,
name: 'webVitals',
},
plugins: configurePlugins({module: false}),
},
{
input: 'dist/modules/attribution/index.js',
output: {
format: 'iife',
file: './dist/web-vitals.attribution.iife.js',
name: 'webVitals',
},
plugins: configurePlugins({module: false}),
},
];

export default configs;
7 changes: 1 addition & 6 deletions src/attribution/onCLS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ const attributeCLS = (metric: CLSMetric): CLSMetricWithAttribution => {
}
}

// Use Object.assign to set property to keep tsc happy.
const metricWithAttribution: CLSMetricWithAttribution = Object.assign(
metric,
{attribution},
);
return metricWithAttribution;
return {...metric, attribution};
};

/**
Expand Down
9 changes: 2 additions & 7 deletions src/attribution/onFCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const attributeFCP = (metric: FCPMetric): FCPMetricWithAttribution => {

if (metric.entries.length) {
const navigationEntry = getNavigationEntry();
const fcpEntry = metric.entries[metric.entries.length - 1];
const fcpEntry = metric.entries.at(-1);

if (navigationEntry) {
const activationStart = navigationEntry.activationStart || 0;
Expand All @@ -51,12 +51,7 @@ const attributeFCP = (metric: FCPMetric): FCPMetricWithAttribution => {
}
}

// Use Object.assign to set property to keep tsc happy.
const metricWithAttribution: FCPMetricWithAttribution = Object.assign(
metric,
{attribution},
);
return metricWithAttribution;
return {...metric, attribution};
};

/**
Expand Down
11 changes: 4 additions & 7 deletions src/attribution/onINP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ const groupEntriesByRenderTime = (entry: PerformanceEventTiming) => {

// Iterate over all previous render times in reverse order to find a match.
// Go in reverse since the most likely match will be at the end.
for (const potentialGroup of pendingEntriesGroups) {
for (let i = pendingEntriesGroups.length - 1; i >= 0; i--) {
const potentialGroup = pendingEntriesGroups[i];

// If a group's render time is within 8ms of the entry's render time,
// assume they were part of the same frame and add it to the group.
if (Math.abs(renderTime - potentialGroup.renderTime) <= 8) {
Expand Down Expand Up @@ -285,12 +287,7 @@ const attributeINP = (metric: INPMetric): INPMetricWithAttribution => {
loadState: getLoadState(firstEntry.startTime),
};

// Use Object.assign to set property to keep tsc happy.
const metricWithAttribution: INPMetricWithAttribution = Object.assign(
metric,
{attribution},
);
return metricWithAttribution;
return {...metric, attribution};
};

/**
Expand Down
10 changes: 3 additions & 7 deletions src/attribution/onLCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const attributeLCP = (metric: LCPMetric): LCPMetricWithAttribution => {
const navigationEntry = getNavigationEntry();
if (navigationEntry) {
const activationStart = navigationEntry.activationStart || 0;
const lcpEntry = metric.entries[metric.entries.length - 1];
// The `metric.entries.length` check ensures there will be an entry.
const lcpEntry = metric.entries.at(-1)!;
const lcpResourceEntry =
lcpEntry.url &&
performance
Expand Down Expand Up @@ -83,12 +84,7 @@ const attributeLCP = (metric: LCPMetric): LCPMetricWithAttribution => {
}
}

// Use Object.assign to set property to keep tsc happy.
const metricWithAttribution: LCPMetricWithAttribution = Object.assign(
metric,
{attribution},
);
return metricWithAttribution;
return {...metric, attribution};
};

/**
Expand Down
7 changes: 1 addition & 6 deletions src/attribution/onTTFB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,7 @@ const attributeTTFB = (metric: TTFBMetric): TTFBMetricWithAttribution => {
};
}

// Use Object.assign to set property to keep tsc happy.
const metricWithAttribution: TTFBMetricWithAttribution = Object.assign(
metric,
{attribution},
);
return metricWithAttribution;
return {...metric, attribution};
};

/**
Expand Down
4 changes: 2 additions & 2 deletions src/lib/bindReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const bindReporter = <MetricName extends MetricType['name']>(
thresholds: MetricRatingThresholds,
reportAllChanges?: boolean,
) => {
let prevValue: number;
let prevValue: number = -1;
let delta: number;
return (forceReport?: boolean) => {
if (metric.value >= 0) {
Expand All @@ -46,7 +46,7 @@ export const bindReporter = <MetricName extends MetricType['name']>(
// value exists (which can happen in the case of the document becoming
// hidden when the metric value is 0).
// See: https://github.com/GoogleChrome/web-vitals/issues/14
if (delta || prevValue === undefined) {
if (delta || prevValue < 0) {
prevValue = metric.value;
metric.delta = delta;
metric.rating = getRating(metric.value, thresholds);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/getSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const getSelector = (node: Node | null | undefined) => {
}
node = el.parentNode;
}
} catch (err) {
} catch {
// Do nothing...
}
return sel;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/initMetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const initMetric = <MetricName extends MetricType['name']>(

return {
name,
value: value,
value,
rating: 'good' as const, // If needed, will be updated when reported. `const` to keep the type from widening to `string`.
delta: 0,
entries,
Expand Down
6 changes: 3 additions & 3 deletions src/lib/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ export const processInteractionEntry = (entry: PerformanceEventTiming) => {
if (!(entry.interactionId || entry.entryType === 'first-input')) return;

// The least-long of the 10 longest interactions.
const minLongestInteraction =
longestInteractionList[longestInteractionList.length - 1];
const minLongestInteraction = longestInteractionList.at(-1);

const existingInteraction = longestInteractionMap.get(entry.interactionId!);

Expand All @@ -105,7 +104,8 @@ export const processInteractionEntry = (entry: PerformanceEventTiming) => {
if (
existingInteraction ||
longestInteractionList.length < MAX_INTERACTIONS_TO_CONSIDER ||
entry.duration > minLongestInteraction.latency
// If the above conditions are false, `minLongestInteraction` will be set.
entry.duration > minLongestInteraction!.latency
) {
// If the interaction already exists, update it. Otherwise create one.
if (existingInteraction) {
Expand Down
6 changes: 2 additions & 4 deletions src/lib/observe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ export const observe = <K extends keyof PerformanceEntryMap>(
callback(list.getEntries() as PerformanceEntryMap[K]);
});
});
po.observe(
Object.assign({type, buffered: true}, opts) as PerformanceObserverInit,
);
po.observe({type, buffered: true, ...opts} as PerformanceObserverInit);
return po;
}
} catch (e) {
} catch {
// Do nothing.
}
return;
Expand Down
5 changes: 3 additions & 2 deletions src/onCLS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,16 @@ export const onCLS = (
// Only count layout shifts without recent user input.
if (!entry.hadRecentInput) {
const firstSessionEntry = sessionEntries[0];
const lastSessionEntry = sessionEntries[sessionEntries.length - 1];
const lastSessionEntry = sessionEntries.at(-1);

// If the entry occurred less than 1 second after the previous entry
// and less than 5 seconds after the first entry in the session,
// include the entry in the current session. Otherwise, start a new
// session.
if (
sessionValue &&
entry.startTime - lastSessionEntry.startTime < 1000 &&
// `lastSessionEntry` will be defined if `sessionsValue` is.
entry.startTime - lastSessionEntry!.startTime < 1000 &&
entry.startTime - firstSessionEntry.startTime < 5000
) {
sessionValue += entry.value;
Expand Down

0 comments on commit ee6cfb2

Please sign in to comment.