Skip to content

Commit

Permalink
Add console error when web-vitals.js is absent
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Mar 22, 2024
1 parent 767c895 commit 4656cbf
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions plugins/optimization-detective/detection.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,24 @@ function od_get_detection_script( string $slug, OD_URL_Metrics_Group_Collection
*/
$detection_time_window = apply_filters( 'od_detection_time_window', 5000 );

$web_vitals_lib_data = require __DIR__ . '/detection/web-vitals.asset.php';
$web_vitals_lib_src = add_query_arg( 'ver', $web_vitals_lib_data['version'], plugin_dir_url( __FILE__ ) . '/detection/web-vitals.js' );
try {
$web_vitals_lib_data = require __DIR__ . '/detection/web-vitals.asset.php';
} catch ( Error $error ) {
$error_message = '[Optimization Detective] ';
$error_message .= sprintf(
/* translators: 1: File path. 2: CLI command. */
__( 'Unable to load %1$s. Please make sure you have run %2$s.', 'optimization-detective' ),
'detection/web-vitals.asset.php',
'`npm install && npm run build:optimization-detective`'
);
if ( WP_DEBUG && WP_DEBUG_DISPLAY ) {
$error_message .= ' ' . __( 'PHP Error:', 'optimization-detective' ) . ' ' . $error->getMessage();
}
return wp_get_inline_script_tag(
sprintf( 'console.error( %s );', wp_json_encode( $error_message ) )
);
}
$web_vitals_lib_src = add_query_arg( 'ver', $web_vitals_lib_data['version'], plugin_dir_url( __FILE__ ) . '/detection/web-vitals.js' );

$current_url = od_get_current_url();
$detect_args = array(
Expand Down

0 comments on commit 4656cbf

Please sign in to comment.