Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 228: Change onINP to also use FID duration when under durationTheshold #231

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/lib/observe.ts
Expand Up @@ -30,19 +30,21 @@ interface PerformanceEntriesHandler {
* try/catch to avoid errors in unsupporting browsers.
*/
export const observe = (
type: string,
types: string[],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered making this string || string[] and adjusting this implementation to support either option... decided it was better to make a single choice but totally understand alterantive.

callback: PerformanceEntriesHandler,
opts?: PerformanceObserverInit,
): PerformanceObserver | undefined => {
try {
if (PerformanceObserver.supportedEntryTypes.includes(type)) {
if (types.every(type => PerformanceObserver.supportedEntryTypes.includes(type))) {
const po: PerformanceObserver = new PerformanceObserver((list) => {
callback(list.getEntries());
});
po.observe(Object.assign({
type,
buffered: true,
}, opts || {}) as PerformanceObserverInit);
for (let type of types) {
po.observe(Object.assign({
type,
buffered: true,
}, opts || {}) as PerformanceObserverInit);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, the same options are passed for all registered types. This is fine for this specific case.

But in general we may want to have [string, PerformanceObserverInit][] (aka array of tuple) instead

}
return po;
}
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/polyfills/interactionCountPolyfill.ts
Expand Up @@ -56,7 +56,7 @@ export const getInteractionCount = () => {
export const initInteractionCountPolyfill = () => {
if ('interactionCount' in performance || po) return;

po = observe('event', updateEstimate, {
po = observe(['event'], updateEstimate, {
type: 'event',
buffered: true,
durationThreshold: 0,
Expand Down
2 changes: 1 addition & 1 deletion src/onCLS.ts
Expand Up @@ -82,7 +82,7 @@ export const onCLS = (onReport: ReportCallback, opts?: ReportOpts) => {
});
};

const po = observe('layout-shift', handleEntries);
const po = observe(['layout-shift'], handleEntries);
if (po) {
report = bindReporter(onReportWrapped, metric, opts.reportAllChanges);

Expand Down
2 changes: 1 addition & 1 deletion src/onFCP.ts
Expand Up @@ -56,7 +56,7 @@ export const onFCP = (onReport: ReportCallback, opts?: ReportOpts) => {
const fcpEntry = window.performance && window.performance.getEntriesByName &&
window.performance.getEntriesByName('first-contentful-paint')[0];

const po = fcpEntry ? null : observe('paint', handleEntries);
const po = fcpEntry ? null : observe(['paint'], handleEntries);

if (fcpEntry || po) {
report = bindReporter(onReport, metric, opts.reportAllChanges);
Expand Down
2 changes: 1 addition & 1 deletion src/onFID.ts
Expand Up @@ -45,7 +45,7 @@ export const onFID = (onReport: ReportCallback, opts?: ReportOpts) => {
(entries as PerformanceEventTiming[]).forEach(handleEntry);
}

const po = observe('first-input', handleEntries);
const po = observe(['first-input'], handleEntries);
report = bindReporter(onReport, metric, opts.reportAllChanges);

if (po) {
Expand Down
4 changes: 2 additions & 2 deletions src/onINP.ts
Expand Up @@ -116,7 +116,7 @@ export const onINP = (onReport: ReportCallback, opts?: ReportOpts) => {

const handleEntries = (entries: Metric['entries']) => {
(entries as PerformanceEventTiming[]).forEach((entry) => {
if (entry.interactionId) {
if (entry.entryType == 'first-input' || entry.interactionId) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only needed until first-input gets a proper interactionID.

For now, this leads to the "10 worst" list being 1 larger. But since web-vitals.js doesn't use that to count number of interactions, it doesn't really matter. The only time it matters is if the first-input duration becomes the 10th largest and ends up evicting another interaction from the list, and then you also have 500 interactions.

processEntry(entry);
}
});
Expand All @@ -130,7 +130,7 @@ export const onINP = (onReport: ReportCallback, opts?: ReportOpts) => {
}
};

const po = observe('event', handleEntries, {
const po = observe(['first-input', 'event'], handleEntries, {
// Event Timing entries have their durations rounded to the nearest 8ms,
// so a duration of 40ms would be any event that spans 2.5 or more frames
// at 60Hz. This threshold is chosen to strike a balance between usefulness
Expand Down
2 changes: 1 addition & 1 deletion src/onLCP.ts
Expand Up @@ -50,7 +50,7 @@ export const onLCP = (onReport: ReportCallback, opts?: ReportOpts) => {
}
};

const po = observe('largest-contentful-paint', handleEntries);
const po = observe(['largest-contentful-paint'], handleEntries);

if (po) {
report = bindReporter(onReport, metric, opts.reportAllChanges);
Expand Down