Skip to content

Conversation

@westonruter
Copy link
Member

This addresses a support forum topic:

Cannot read properties of undefined (reading ‘groups’)

detect.min.js?ver=1.0.0-beta3:1 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘groups’)
at detect (detect.min.js?ver=1.0.0-beta3:1:3066)
at :2:127

The location where this error is occurring:

if ( isDebug ) {
const allUrlMetrics = /** @type Array<UrlMetricDebugData> */ [];
for ( const group of urlMetricGroupCollection.groups ) {

Normally the urlMetricGroupCollection is always populated when WP_DEBUG is true:

if ( WP_DEBUG ) {
$detect_args['urlMetricGroupCollection'] = $group_collection;
}

However, it seems at least in this case here, the HTML had been mutated to remove that object being passed to detect(). So this PR hardens the logic to prevent an error from happening.

Additionally, there is another benefit. Often when debugging Optimization Detective for sites in the wild, I'll use local overrides in Chrome to modify the HTML as follows:

  1. Set isDebug to true
  2. Replace detect.min.js with detect.js

However, when I do this, I then also need to open up detect.js and add another override to comment out this same line that tries to read from the group. By checking first to make sure that the property exists and is an array, then this will no longer be necessary when debugging sites in the field.

@github-actions
Copy link

github-actions bot commented Aug 3, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: westonruter <[email protected]>
Co-authored-by: ShyamGadde <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@westonruter westonruter added [Type] Bug An existing feature is broken [Plugin] Optimization Detective Issues for the Optimization Detective plugin labels Aug 3, 2025
@github-project-automation github-project-automation bot moved this to Not Started/Backlog 📆 in WP Performance 2025 Aug 3, 2025
@westonruter westonruter moved this from Not Started/Backlog 📆 to Code Review 👀 in WP Performance 2025 Aug 3, 2025
Copy link
Contributor

@ShyamGadde ShyamGadde left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

@westonruter westonruter merged commit 2fd95b6 into trunk Aug 4, 2025
12 of 16 checks passed
@westonruter westonruter deleted the fix/od-debug-mode-without-groups branch August 4, 2025 06:27
@github-project-automation github-project-automation bot moved this from Code Review 👀 to Done 😃 in WP Performance 2025 Aug 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Bug An existing feature is broken

Projects

Status: Done 😃

Development

Successfully merging this pull request may close these issues.

3 participants