question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
felixarntzcommented, Aug 17, 2021

@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 🤔

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: Failed to construct 'URL': Invalid URL - WordPress.org
I just tried accessing the Site Kit dashboard after updating to WordPress 5.8 and I was presented with the “Site Kit encountered an...
Read more >
Setup - Site Kit by Google
This error occurs when necessary query parameters are stripped from the URLs that redirect you from Google back to your WordPress site to...
Read more >
Failed to construct 'URL': Invalid URL in chrome extension v3 ...
So, i see the possible solution(how to get real error message). I've set breakpoint on failed line and check the runtimeError message.
Read more >
How to Fix: Default Kit Missing Error - Elementor
The way to solve this error is navigating to Elementor > Tools > General Tab. Here you will see an option to Recreate...
Read more >
Admin Authentication API Errors | Firebase - Google
auth/internal-error, The Authentication server encountered an unexpected error ... auth/invalid-continue-uri, The continue URL must be a valid URL string.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found