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

Allow extensions to opt-in to using the web-vitals attribution build via the od_use_web_vitals_attribution_build filter #1759

Merged
merged 45 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
fc95f4a
Bundle web-vitals attribution build
swissspidy Dec 18, 2024
651577e
Allow loading web-vitals attribution build
swissspidy Dec 18, 2024
ff3f120
Collect some INP data for testing
swissspidy Dec 18, 2024
9dd8159
Add debugging helper for LCP elements
swissspidy Dec 18, 2024
12a3494
some lint fixes
swissspidy Dec 18, 2024
be0be49
Add INP elements with JS
swissspidy Dec 18, 2024
e9bbe99
Apply suggestions from code review
swissspidy Dec 19, 2024
ac4d153
Add missing return statement
swissspidy Dec 19, 2024
098191c
Lint fixes
swissspidy Dec 19, 2024
9288025
Clarify comment
swissspidy Dec 19, 2024
2f507a0
PHPStan fixes
swissspidy Dec 19, 2024
8ead9ae
Multiline comment
swissspidy Dec 19, 2024
ad09dea
Merge branch 'trunk' into try/1736-debug-detective
swissspidy Dec 19, 2024
3e4d648
Add empty line
swissspidy Dec 19, 2024
c8b490b
Merge branch 'trunk' into try/1736-debug-detective
swissspidy Jan 10, 2025
36430ad
Only load assets if admin bar is showing
swissspidy Jan 10, 2025
a229ba6
Extract `od_get_group_collection()` helper
swissspidy Jan 10, 2025
ba48bd9
Don't add visitor if not showing admin bar
swissspidy Jan 10, 2025
581ffbc
Add `additionalProperties`
swissspidy Jan 10, 2025
eabd8c8
Fix anchors and popovers
swissspidy Jan 10, 2025
ebfea3f
Re-enable css
swissspidy Jan 10, 2025
c2d1a9f
Fix test_get_json_schema
westonruter Jan 13, 2025
6d95ce1
Prevent warning if INP data is missing
swissspidy Jan 13, 2025
5de84c6
Remove admin bar condition in admin bar cb
swissspidy Jan 13, 2025
adf3ae5
Styling adjustments
swissspidy Jan 13, 2025
e7432ed
Remove dev mode condition
swissspidy Jan 13, 2025
d552366
Move INP logic to tag visitor too
swissspidy Jan 13, 2025
2aaad52
Undo `od_get_group_collection`
swissspidy Jan 13, 2025
39f8336
Lint fixes
swissspidy Jan 13, 2025
1d900d2
Reuse existing anchor name
swissspidy Jan 13, 2025
efc1aa6
phpstan fixes
swissspidy Jan 13, 2025
b63b9d2
lint fix
swissspidy Jan 13, 2025
6d848d5
Merge branch 'trunk' into try/1736-debug-detective
swissspidy Jan 13, 2025
2f162f2
Remove extracted code
swissspidy Jan 13, 2025
89b718a
Merge branch 'trunk' into try/1736-debug-detective
swissspidy Jan 13, 2025
d32ffc0
Undo tests change too
swissspidy Jan 13, 2025
2802b57
Remove JS parts now as well
swissspidy Jan 14, 2025
5fd6891
Undo type import
swissspidy Jan 14, 2025
8dd8af6
Don't conditionally load by default
swissspidy Jan 14, 2025
c6f53fa
Remove newline
swissspidy Jan 14, 2025
fd7fa9f
Add jsdoc description
westonruter Jan 14, 2025
3614c13
Add typing for attribution report functions
westonruter Jan 14, 2025
3a30027
Add docs for the od_use_web_vitals_attribution_build filter
westonruter Jan 14, 2025
a75dd53
Add test for od_use_web_vitals_attribution_build filter
westonruter Jan 14, 2025
9600078
fixup! Add docs for the od_use_web_vitals_attribution_build filter
westonruter Jan 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions plugins/optimization-detective/detect.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* @typedef {import("web-vitals").LCPMetric} LCPMetric
* @typedef {import("web-vitals").LCPMetricWithAttribution} LCPMetricWithAttribution
* @typedef {import("./types.ts").ElementData} ElementData
* @typedef {import("./types.ts").OnTTFBFunction} OnTTFBFunction
* @typedef {import("./types.ts").OnFCPFunction} OnFCPFunction
Expand Down Expand Up @@ -490,13 +491,17 @@ export default async function detect( {
} );
}

/** @type {LCPMetric[]} */
/** @type {(LCPMetric|LCPMetricWithAttribution)[]} */
const lcpMetricCandidates = [];

// Obtain at least one LCP candidate. More may be reported before the page finishes loading.
await new Promise( ( resolve ) => {
onLCP(
( /** @type LCPMetric */ metric ) => {
/**
*
* @param {LCPMetric|LCPMetricWithAttribution} metric
*/
( metric ) => {
Copy link
Member

Choose a reason for hiding this comment

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

Could this go back to being defined inline? The type is picked up for me in PhpStorm:

image

Suggested change
/**
*
* @param {LCPMetric|LCPMetricWithAttribution} metric
*/
( metric ) => {
( /** @type {LCPMetric|LCPMetricWithAttribution} */ metric ) => {

Copy link
Member Author

Choose a reason for hiding this comment

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

But your PhpStorm clearly only sees LCPMetric, not LCPMetricWithAttribution

Copy link
Member

Choose a reason for hiding this comment

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

Good point!

What's strange to me is why this typing is even needed.

With 3614c13 I added more typing for the attribution report functions. Then PHPStorm reports that the arg is detected as a LCPMetricWithAttribution:

image

But TypeScript is giving a warning about it being implicit any:

image

Nevertheless, onLCP is defined as being either OnLCPFunction or OnLCPWithAttributionFunction:

image

So I would have thought that this would be later be reflected in the type of the arg here. But instead it is showing up as any, even though the common ReportOpts arg is carried through:

image

lcpMetricCandidates.push( metric );
resolve();
},
Expand All @@ -511,6 +516,7 @@ export default async function detect( {

// Stop observing.
disconnectIntersectionObserver();

swissspidy marked this conversation as resolved.
Show resolved Hide resolved
if ( isDebug ) {
log( 'Detection is stopping.' );
}
Expand Down
16 changes: 15 additions & 1 deletion plugins/optimization-detective/detection.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,22 @@
* @param OD_URL_Metric_Group_Collection $group_collection URL Metric group collection.
*/
function od_get_detection_script( string $slug, OD_URL_Metric_Group_Collection $group_collection ): string {
$use_attribution_build = WP_DEBUG || wp_is_development_mode( 'plugin' );

/**
* Filters whether to use the web-vitals.js build with attribution.
*
* @since n.e.x.t
*
* @param bool $use_attribution_build Whether to use the attribution build.
*/
$use_attribution_build = (bool) apply_filters( 'od_use_web_vitals_attribution_build', $use_attribution_build );
swissspidy marked this conversation as resolved.
Show resolved Hide resolved

$web_vitals_lib_data = require __DIR__ . '/build/web-vitals.asset.php';
$web_vitals_lib_src = plugins_url( add_query_arg( 'ver', $web_vitals_lib_data['version'], 'build/web-vitals.js' ), __FILE__ );
$web_vitals_lib_src = $use_attribution_build ?
plugins_url( 'build/web-vitals-attribution.js', __FILE__ ) :

Check warning on line 86 in plugins/optimization-detective/detection.php

View check run for this annotation

Codecov / codecov/patch

plugins/optimization-detective/detection.php#L86

Added line #L86 was not covered by tests
plugins_url( 'build/web-vitals.js', __FILE__ );
$web_vitals_lib_src = add_query_arg( 'ver', $web_vitals_lib_data['version'], $web_vitals_lib_src );

/**
* Filters the list of extension script module URLs to import when performing detection.
Expand Down
5 changes: 5 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ const optimizationDetective = ( env ) => {
to: `${ destination }/build/web-vitals.js`,
info: { minimized: true },
},
{
from: `${ source }/dist/web-vitals.attribution.js`,
to: `${ destination }/build/web-vitals-attribution.js`,
info: { minimized: true },
},
{
from: `${ source }/package.json`,
to: `${ destination }/build/web-vitals.asset.php`,
Expand Down
Loading