site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Site Kit encountered an error: Error: Failed to construct ‘URL’: Invalid URL

Open jamesozzie opened this issue 5 years ago • 16 comments

Bug Description

After updating to WordPress 5.8 @westonruter is encountering the following dashboard error appeared when accessing the the primary Site Kit screen. (WP support forum topic)

Site Kit encountered an error

Failed to construct 'URL': Invalid URL

    in DetailsPermaLinks
    in Component
    in div
    in td
    in tr
    in tbody
    in table
    in div
    in ReportTable
    in div
    in div
    in TableOverflowContainer
    in div
    in div
    in Widget
    in WithWidgetSlug(Widget)
    in DashboardPopularPagesWidget
    in WhenAnalyticsActive(DashboardPopularPagesWidget)
    in WidgetRenderer
    in div
    in Cell
    in WidgetCellWrapper
    in div
    in Row
    in div
    in div
    in Grid
    in WidgetAreaRenderer
    in div
    in WidgetContextRenderer
    in DashboardApp
    in GoogleSitekitDashboard
    in RestoreSnapshots
    in ErrorHandler
    in Root

This error remans when revisiting the Site Kit Summary dashboard widget.

One possible cause could be additional plugins using the Google Charts library, similar to other JS errors as per #3132.

We did encounter the same error previously in support (#1855) although this occurred for the impacted using during AdSense setup, with no further details provided or instances reported.

There are also references in the Lighthouse repository of the same specific error although as @westonruter pointed out the stack trace points includes DashboardPopularPagesWidget and WhenAnalyticsActive references.

Additional Context


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • Site Kit calls instantiating a new URL in JS should be wrapped in a "try-catch" block if the value may not be a valid URL.
    • In case of an error an appropriate fallback should be used, e.g. an empty string or false/null.
  • While it would technically be safest to do it everywhere in production code, we should at least do it in instances where the value passed to it does not come from within Site Kit, i.e. it comes from a Google API.
    • We can reliably assume that e.g. a siteURL value from Site Kit or location.href from the browser are valid URLs/paths.
    • Any URL checked with isURL from @wordpress/url is also valid because it does the same check internally
    • However we cannot reliably assume that all values from external APIs like Search Console, Analytics, or AdSense are valid URLs/paths.

Implementation Brief

The getURLPath and getFullURL utilities inside assets/js/util/urls.js need to be refactored as follows.

getURLPath

  • Wrap the call to new URL in a try-catch block
  • Inside the catch, return null
  • Inside each consumer, refactor the logic at the getURLPath callsite, so that if it is falsey the path is not appended to the report arguments. For example, in DashboardAllTrafficWidget:
    // Before:
      if ( entityURL ) {
      	reportArgs[ 'explorer-table.plotKeys' ] = '[]';
      	reportArgs[ '_r.drilldown' ] = `analytics.pagePath:${ getURLPath(
      		entityURL
      	) }`;
      }
    // After:
      const path = !! entityURL && getURLPath( entityURL );
      if ( path ) {
      	reportArgs[ 'explorer-table.plotKeys' ] = '[]';
      	reportArgs[ '_r.drilldown' ] = `analytics.pagePath:${ path }`;
      }
    
    You'll need to update the following files:
    assets/js/modules/analytics/components/dashboard/DashboardBounceRateWidget.js
    80:			? `analytics.pagePath:${ getURLPath( url ) }`
    
    assets/js/modules/analytics/components/dashboard/DashboardAllTrafficWidget/index.js
    188:		reportArgs[ '_r.drilldown' ] = `analytics.pagePath:${ getURLPath(
    
    assets/js/modules/analytics/components/dashboard/DashboardSearchVisitorsWidget.js
    101:		drilldowns.push( `analytics.pagePath:${ getURLPath( url ) }` );
    
  • Update the unit tests for this function

getFullURL

  • Wrap the call to new URL in a try-catch block
  • Inside the catch, return a string which is a concatenation of the two params, i.e. siteURL + path
  • Update the unit tests for this function

Test Coverage

  • Unit tests for the two utilities should be updated as described above.

Visual Regression Changes

  • None anticipated.

QA Brief

  • Check getURLPath and getFullURL functions to make sure that they wrap new URL(...) calls with try/catch block.
  • Check getURLPath function usage to verify that all calls account for possible NULL results.

Changelog entry

  • Fix a potential error due to report data associated with an invalid URL.

jamesozzie avatar Jul 23 '21 13:07 jamesozzie

Disconnecting Analytics did stop the error from happening. However, once I reconnected Analytics then the error returned.

Also, I did try using Health Check & Troubleshooting to isolate Site Kit as the only active plugin, but the error persisted.

I tried clearing the persistent object cache but the issue still occurs.

westonruter avatar Jul 23 '21 17:07 westonruter

Interesting that the same occurs once reconnecting Analytics. Can you confirm if the same occurs if you select the option to create a new Analytics account as opposed to selecting a pre-existing Analytics account and property?

In the meantime I'll do some additional checks on my side.

jamesozzie avatar Jul 26 '21 12:07 jamesozzie

@westonruter Are there any odd-looking URL values in your Analytics top pages when checking your Analytics data? Maybe we are missing a scenario there and need more “forgiving” logic. (copied from similar wp.org forum reply)

felixarntz avatar Jul 27 '21 15:07 felixarntz

Unable to recreate this issue & performed following activities:

  • Installed Site Kit 1.35.0
  • Configured Search Console with the newly created Google account
  • Setup Google Analytics
  • Updated Site Kit to latest version 1.37.0

abdullah1908 avatar Jul 27 '21 16:07 abdullah1908

Are there any odd-looking URL values in your Analytics top pages when checking your Analytics data?

Yes, there are:

image

I am using the analytics snippet as provided by Site Kit. I'm seeing double trailing slashes for some reason. Perhaps that's the cause of the JS error?

image

westonruter avatar Jul 27 '21 17:07 westonruter

@westonruter Yes that looks like the cause of the error. Curious how the URLs come up though. Are all of them with double slashes? Has that only started happening recently?

felixarntz avatar Jul 27 '21 17:07 felixarntz

To be honest, I haven't looked at my analytics data in a long time so I don't know if it's a recent occurrence. Nevertheless, all of the URLs appear with the double slashes at the end of the path.

westonruter avatar Jul 27 '21 17:07 westonruter

@felixarntz Just checking for an update here.

mxbclang avatar Aug 17 '21 13:08 mxbclang

@bethanylang @aaemnnosttv @westonruter I've defined the ACs for this issue around fixing the JS error, where our code should be more defensively written. However, the weird situation with double slashes would still be a problem - however, I don't think we should add logic to specifically fix double slashes, since that would open a can of worms around resolving any sort of potential weirdness with URLs stored in Analytics. Any idea why those double-slashed URLs could happen? I can't imagine how that is caused by Site Kit since we merely inject the Analytics snippet 🤔

felixarntz avatar Aug 17 '21 20:08 felixarntz

@johnPhillips Just one really minor thing: is there any reason not to return null instead of false? Returning a boolean here just… seems a bit wrong. The idea that the input can't create a URL and thus returns null seems to make a bit more semantic sense to be than it returning false.

Other than that this looks good, just let me know about the return value—if we can change it to null that'd be grand, and then this can go into the backlog 👍🏻

tofumatt avatar Aug 26 '21 22:08 tofumatt

@tofumatt I think I chose false originally because of the AC, but I agree that null would be the better choice. I've updated the IB accordingly.

johnPhillips avatar Aug 27 '21 08:08 johnPhillips

Ah, fair enough, I missed that. Since it wasn't particular about the value I've added null, as the intent is just to return something indicating the supplied URL wasn't usable.

IB ✅

tofumatt avatar Aug 27 '21 14:08 tofumatt

QA ✅

  • Check getURLPath and getFullURL functions to make sure that they wrap new URL(...) calls with try/catch block. ✅
  • Check getURLPath function usage to verify that all calls account for possible NULL results. ✅

johnPhillips avatar Sep 02 '21 15:09 johnPhillips

I can confirm the issue is fixed in terms of the error. I can access the Site Kit dashboard now.

Nevertheless, I still do see the doubled trailing slashes in the URLs. But that seems like an analytics configuration problem. This seems like a Site Kit concern though, as I'm using the analytics code as added by Site Kit.

westonruter avatar Sep 14 '21 04:09 westonruter

Hi @westonruter. Could you please create a screenshot of your issue and start a new ticket for it referencing this one? It will help us to figure out the root of the problem and fix it.

eugene-manuilov avatar Sep 14 '21 06:09 eugene-manuilov

@eugene-manuilov Here you go: https://github.com/google/site-kit-wp/issues/4036

westonruter avatar Sep 14 '21 16:09 westonruter