Cannot read properties of undefined (reading 'load')
See original GitHub issueBug Description
There have been four users who referenced the below dashboard error, all within the same week:
Cannot read properties of undefined (reading 'load')
- https://wordpress.org/support/topic/site-kit-encountered-an-error-49/ | Open | SH info
- https://wordpress.org/support/topic/error-detected-3/#post-14888496 | Open | No SH info at this point
- https://wordpress.org/support/topic/cannot-read-properties-of-undefined-reading-load | Open | No SH info at this point
While awaiting details from one impacted user on the first report this only occurs when selecting specific reporting periods on the primary Site Kit dashboard - all expect the 28 day reporting period. This occurs consistently for this user, and doesn’t occur in troubleshooting mode (using the Health Check plugin).
This is likely as a result of a plugin or theme conflict. Awaiting for checks and details from impacted users
Additional Context
- Plugin Version: 1.41.0
- This only started to occur from 1.41.0
- No previous reports until last week
- One user (not the original reporter) stated that this occurred once PageSpeed Insights was activated, within one of the support forum threads. Once they “cleared the error” it no longer occurred
- The issue does still remain incognito mode
- The issue no longer occurs in troubleshooting mode using the Health Check & Troubleshooting plugin for one user (performing other checks at present to try and pinpoint the issue)
- No browser console errors provided yet
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- All Site Kit dashboard screens should proactively define the
window.google
as undefined to avoid a potential error when charts are initializing. See https://github.com/google/site-kit-wp/issues/4074#issuecomment-967364462 for more information
Original AC
- The Google charts library should be rolled back to the hard-coded
49
version, as the newer versions are still causing a high number of errors.- In a future issue we should explore updating again but rule out that those errors happen. It will probably be a much larger effort, but this is a critical problem that we need to get out for the upcoming release.
Implementation Brief
- Add a new JS module
assets/js/util/initialize-google-global
- Document this as a side-effect import with no exports
- Conditionally set the
google
global toundefined
ifwindow
does nothasOwnProperty('google')
- Import this as the first import in
assets/js/components/GoogleChart.js
and document similar to https://github.com/google/site-kit-wp/blob/5c556a874282335f4936f4f9a1a698e0a19c1bd6/.storybook/preview.js#L31-L32
Original IB
- Change
GoogleChart
to use version49
again (undoing the change here https://github.com/google/site-kit-wp/pull/3881/files#diff-e632e2b02991386dad9213942af598e92c8fbd29e11e8fe4b58b0533fcf8698d) - Merge #4098
Test Coverage
- Test rendering two
GoogleCharts
with a<div id="google" />
element on the page, the second one rendering after the first is visible/ready to ensure loading after the fact does not raise an error
QA Brief
- Install and activate User Profile Picture.
- Visit SiteKit Dashboard and make sure the charts are loading without the error. (in UI or console).
Changelog entry
- Fix a potential conflict with
google
global when loading Google charts.
Issue Analytics
- State:
- Created 2 years ago
- Comments:43 (7 by maintainers)
Top Results From Across the Web
Cannot Read Property of Undefined in JavaScript - Rollbar
TypeError : Cannot read property of undefined occurs when a property is read or a function is called on an undefined variable.
Read more >TypeError: Cannot read properties of undefined (reading 'id')
I got the error "Cannot read properties of undefined (reading 'spec')" when attempting to install a local npm module with npm install [path ......
Read more >Uncaught TypeError: Cannot read property of undefined In
Uncaught TypeError : Cannot read property of undefined error occurs in Chrome when you read a property or call a method on an...
Read more >[Solved] Cannot read Properties of Undefined in JavaScript
The "Cannot read properties of undefined" error occurs when you try to access a property or a method on a variable that stores...
Read more >Cannot read properties of undefined (reading 'load')
Cannot read properties of undefined (reading 'load') ... You can close the ticket, I have cleared all cache from my site and it...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This occurs with this Site Kit and the User Profile Picture plugin active (recording).
This plugin is evident on other users sites from checking their Site Health info.
I don’t see any reference to the Google Charts Library API within the plugin however the plugin is open source. (GitHub repo)
Site Health info
Console Errors
In order to recreate this issue setup a site and install and active the User Profile Photo plugin. A workaround for this impacted at present is to temporarily deactivate the User Profile plugin when viewing the Site Kit dashboards.
@aaemnnosttv Do you want me to share credentials for the impacted site? I don’t see any independent scripts output on wp-admin from the plugin however I’m happy to review the plugin further.
@felixarntz after digging into this a bit more the problem seems to be rooted in the charts loader script itself. I’ve raised this upstream where it may be fixable without any update to Site Kit since
loader.js
is unversioned.https://github.com/google/google-visualization-issues/issues/2936
The problem in our case (in conjunction with this bug) is due to how the
react-google-charts
library works in that it renders aGoogleChartLoader
with everyGoogleChart
. TheGoogleChartLoader
is where the error is raised. There isn’t inherently a problem with this loader being rendered multiple times; it only adds the loader script once, but it does register anonLoad
callback for each instance which in turn will callgoogle.charts.load(...
for each one due to the wayreact-load-script
works, which essentially queues all theonLoad
andonError
callbacks for a given URL when rendered multiple times. Nothing particularly wrong with that either, but what happens is that whenloader.js
is loaded with an#google
element on the page, the loader ends up assigning its properties to the DOM element instance referenced on the window. Then when the charts are actually loaded (google.charts.load
), somewhere between this and when the chartsonLoadCallback
is called,window.google
is properly assigned which breaks the association with the element previously referenced, so what would normally be onwindow.google
is split across two separate objects. If we were only rendering a single chart, this likely be unnoticeable because the first call togoogle.charts.load
will always work.However, due to the multiple calls to
google.charts.load
via theGoogleChartLoader
, this is where we run the risk of the error shown in this issue. The only requirement is that the charts don’t all load at the same time (i.e. cold cache). It’s much less likely for this to happen with a primed request cache due to the time between charts loading and the specific charts version being loaded (as a result ofgoogle.charts.load(version)
.Again, the issue seems to be rooted in the charts
loader.js
(see the issue referenced above where I’ve included a repo which shows this reproduced on a simple html page) in which case we may not need to update anything here to fix it for everyone, although that might depend on how long it takes for a fix to be implemented upstream.