Skip to content

Commit 47d06dc

Browse files
committed
Export web-vitals callback functions to extensions rather than webVitalsLibrarySrc
1 parent 3ac0253 commit 47d06dc

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

plugins/image-prioritizer/detect.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@ function warn( ...message ) {
6363
* @type {InitializeCallback}
6464
* @param {InitializeArgs} args Args.
6565
*/
66-
export async function initialize( { isDebug, webVitalsLibrarySrc } ) {
67-
const { onLCP } = await import( webVitalsLibrarySrc );
66+
export async function initialize( { isDebug, onLCP } ) {
6867
onLCP(
69-
( /** @type {LCPMetric} */ metric ) => {
68+
( metric ) => {
7069
handleLCPMetric( metric, isDebug );
7170
},
7271
{

plugins/optimization-detective/detect.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/**
22
* @typedef {import("web-vitals").LCPMetric} LCPMetric
33
* @typedef {import("./types.ts").ElementData} ElementData
4+
* @typedef {import("./types.ts").OnTTFBFunction} OnTTFBFunction
5+
* @typedef {import("./types.ts").OnFCPFunction} OnFCPFunction
6+
* @typedef {import("./types.ts").OnLCPFunction} OnLCPFunction
7+
* @typedef {import("./types.ts").OnINPFunction} OnINPFunction
8+
* @typedef {import("./types.ts").OnCLSFunction} OnCLSFunction
49
* @typedef {import("./types.ts").URLMetric} URLMetric
510
* @typedef {import("./types.ts").URLMetricGroupStatus} URLMetricGroupStatus
611
* @typedef {import("./types.ts").Extension} Extension
@@ -335,6 +340,14 @@ export default async function detect( {
335340
{ once: true }
336341
);
337342

343+
const {
344+
/** @type OnTTFBFunction */ onTTFB,
345+
/** @type OnFCPFunction */ onFCP,
346+
/** @type OnLCPFunction */ onLCP,
347+
/** @type OnINPFunction */ onINP,
348+
/** @type OnCLSFunction */ onCLS,
349+
} = await import( webVitalsLibrarySrc );
350+
338351
// TODO: Does this make sense here?
339352
// Prevent detection when page is not scrolled to the initial viewport.
340353
if ( doc.documentElement.scrollTop > 0 ) {
@@ -368,7 +381,11 @@ export default async function detect( {
368381
if ( extension.initialize instanceof Function ) {
369382
const initializePromise = extension.initialize( {
370383
isDebug,
371-
webVitalsLibrarySrc,
384+
onTTFB,
385+
onFCP,
386+
onLCP,
387+
onINP,
388+
onCLS,
372389
} );
373390
if ( initializePromise instanceof Promise ) {
374391
extensionInitializePromises.push( initializePromise );
@@ -454,8 +471,6 @@ export default async function detect( {
454471
} );
455472
}
456473

457-
const { onLCP } = await import( webVitalsLibrarySrc );
458-
459474
/** @type {LCPMetric[]} */
460475
const lcpMetricCandidates = [];
461476

plugins/optimization-detective/types.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// h/t https://stackoverflow.com/a/59801602/93579
22
type ExcludeProps< T > = { [ k: string ]: any } & { [ K in keyof T ]?: never };
33

4+
import { onTTFB, onFCP, onLCP, onINP, onCLS } from 'web-vitals';
5+
46
export interface ElementData {
57
isLCP: boolean;
68
isLCPCandidate: boolean;
@@ -28,9 +30,19 @@ export interface URLMetricGroupStatus {
2830
complete: boolean;
2931
}
3032

33+
export type OnTTFBFunction = typeof onTTFB;
34+
export type OnFCPFunction = typeof onFCP;
35+
export type OnLCPFunction = typeof onLCP;
36+
export type OnINPFunction = typeof onINP;
37+
export type OnCLSFunction = typeof onCLS;
38+
3139
export type InitializeArgs = {
3240
readonly isDebug: boolean;
33-
readonly webVitalsLibrarySrc: string;
41+
readonly onTTFB: OnTTFBFunction;
42+
readonly onFCP: OnFCPFunction;
43+
readonly onLCP: OnLCPFunction;
44+
readonly onINP: OnINPFunction;
45+
readonly onCLS: OnCLSFunction;
3446
};
3547

3648
export type InitializeCallback = ( args: InitializeArgs ) => Promise< void >;

0 commit comments

Comments
 (0)